-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2341 from chipaca/envbool
many: unify boolean env var handling
- Loading branch information
Showing
10 changed files
with
119 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,7 @@ func ExecInCoreSnap() { | |
return | ||
} | ||
|
||
if os.Getenv(key) != "1" { | ||
if !osutil.GetenvBool(key) { | ||
return | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2016 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package osutil | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
) | ||
|
||
// GetenvBool returns whether the given key may be considered "set" in the environment | ||
// (i.e. it is set to one of "1", "true", etc) | ||
func GetenvBool(key string) bool { | ||
val := os.Getenv(key) | ||
if val == "" { | ||
return false | ||
} | ||
|
||
b, err := strconv.ParseBool(val) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2016 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package osutil_test | ||
|
||
import ( | ||
"os" | ||
|
||
"gopkg.in/check.v1" | ||
|
||
"github.com/snapcore/snapd/osutil" | ||
) | ||
|
||
type envSuite struct{} | ||
|
||
var _ = check.Suite(&envSuite{}) | ||
|
||
func (s *envSuite) TestGetenvBoolTrue(c *check.C) { | ||
key := "__XYZZY__" | ||
os.Unsetenv(key) | ||
|
||
for _, s := range []string{ | ||
"1", "t", "TRUE", // etc | ||
} { | ||
os.Setenv(key, s) | ||
c.Assert(os.Getenv(key), check.Equals, s) | ||
c.Check(osutil.GetenvBool(key), check.Equals, true, check.Commentf(s)) | ||
} | ||
} | ||
|
||
func (s *envSuite) TestGetenvBoolFalse(c *check.C) { | ||
key := "__XYZZY__" | ||
os.Unsetenv(key) | ||
c.Assert(osutil.GetenvBool(key), check.Equals, false) | ||
|
||
for _, s := range []string{ | ||
"", "0", "f", "FALSE", // etc | ||
} { | ||
os.Setenv(key, s) | ||
c.Assert(os.Getenv(key), check.Equals, s) | ||
c.Check(osutil.GetenvBool(key), check.Equals, false, check.Commentf(s)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters