Skip to content

Commit

Permalink
Merge pull request #2341 from chipaca/envbool
Browse files Browse the repository at this point in the history
many: unify boolean env var handling
  • Loading branch information
chipaca committed Nov 23, 2016
2 parents 8f62673 + a606cf6 commit 5221eba
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 16 deletions.
4 changes: 2 additions & 2 deletions asserts/sysdb/trusted.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ package sysdb

import (
"fmt"
"os"

"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/osutil"
)

const (
Expand Down Expand Up @@ -130,7 +130,7 @@ func init() {
// Trusted returns a copy of the current set of trusted assertions as used by Open.
func Trusted() []asserts.Assertion {
trusted := []asserts.Assertion(nil)
if os.Getenv("SNAPPY_USE_STAGING_STORE") != "1" {
if !osutil.GetenvBool("SNAPPY_USE_STAGING_STORE") {
trusted = append(trusted, trustedAssertions...)
} else {
trusted = append(trusted, trustedStagingAssertions...)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ExecInCoreSnap() {
return
}

if os.Getenv(key) != "1" {
if !osutil.GetenvBool(key) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func bootstrapToRootDir(sto Store, model *asserts.Model, opts *Options, local *l
f := makeFetcher(sto, &DownloadOptions{}, db)

if err := f.Save(model); err != nil {
if os.Getenv("UBUNTU_IMAGE_SKIP_COPY_UNVERIFIED_MODEL") == "" {
if !osutil.GetenvBool("UBUNTU_IMAGE_SKIP_COPY_UNVERIFIED_MODEL") {
return fmt.Errorf("cannot fetch and check prerequisites for the model assertion: %v", err)
} else {
logger.Noticef("Cannot fetch and check prerequisites for the model assertion, it will not be copied into the image: %v", err)
Expand Down
4 changes: 3 additions & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"log/syslog"
"os"
"sync"

"github.com/snapcore/snapd/osutil"
)

// A Logger is a fairly minimal logging tool.
Expand Down Expand Up @@ -109,7 +111,7 @@ func (l *ConsoleLog) Debug(msg string) {
s := "DEBUG: " + msg
l.sys.Output(3, s)

if os.Getenv("SNAPD_DEBUG") != "" {
if osutil.GetenvBool("SNAPD_DEBUG") {
l.log.Output(3, s)
}
}
Expand Down
41 changes: 41 additions & 0 deletions osutil/env.go
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
}
59 changes: 59 additions & 0 deletions osutil/env_test.go
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))
}
}
3 changes: 2 additions & 1 deletion overlord/devicestate/devicemgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/configstate"
Expand Down Expand Up @@ -359,7 +360,7 @@ func (m *DeviceManager) Stop() {
}

func useStaging() bool {
return os.Getenv("SNAPPY_USE_STAGING_STORE") == "1"
return osutil.GetenvBool("SNAPPY_USE_STAGING_STORE")
}

func deviceAPIBaseURL() string {
Expand Down
2 changes: 1 addition & 1 deletion snap/squashfs/squashfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *Snap) Install(targetPath, mountDir string) error {
// snap when we "install" a squashfs snap in the tests.
// We can not mount it for real in the tests, so we just unpack
// it to the location which is good enough for the tests.
if os.Getenv("SNAPPY_SQUASHFS_UNPACK_FOR_TESTS") != "" {
if osutil.GetenvBool("SNAPPY_SQUASHFS_UNPACK_FOR_TESTS") {
if err := s.Unpack("*", mountDir); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion store/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (loggerSuite) TearDownTest(c *check.C) {
}

func (s *loggerSuite) SetUpTest(c *check.C) {
os.Setenv("SNAPD_DEBUG", "yes")
os.Setenv("SNAPD_DEBUG", "true")
s.logbuf = bytes.NewBuffer(nil)
l, err := logger.NewConsoleLog(s.logbuf, logger.DefaultFlags)
c.Assert(err, check.IsNil)
Expand Down
16 changes: 8 additions & 8 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,20 @@ func getStructFields(s interface{}) []string {
}

func useDeltas() bool {
return os.Getenv("SNAPD_USE_DELTAS_EXPERIMENTAL") == "1"
return osutil.GetenvBool("SNAPD_USE_DELTAS_EXPERIMENTAL")
}

func useStaging() bool {
return os.Getenv("SNAPPY_USE_STAGING_STORE") == "1"
return osutil.GetenvBool("SNAPPY_USE_STAGING_STORE")
}

func cpiURL() string {
if useStaging() {
return "https://search.apps.staging.ubuntu.com/api/v1/"
}
// FIXME: this will become a store-url assertion
if os.Getenv("SNAPPY_FORCE_CPI_URL") != "" {
return os.Getenv("SNAPPY_FORCE_CPI_URL")
if u := os.Getenv("SNAPPY_FORCE_CPI_URL"); u != "" {
return u
}

return "https://search.apps.ubuntu.com/api/v1/"
Expand All @@ -255,8 +255,8 @@ func authLocation() string {
}

func authURL() string {
if os.Getenv("SNAPPY_FORCE_SSO_URL") != "" {
return os.Getenv("SNAPPY_FORCE_SSO_URL")
if u := os.Getenv("SNAPPY_FORCE_SSO_URL"); u != "" {
return u
}
return "https://" + authLocation() + "/api/v2"
}
Expand All @@ -266,8 +266,8 @@ func assertsURL() string {
return "https://assertions.staging.ubuntu.com/v1/"
}

if os.Getenv("SNAPPY_FORCE_SAS_URL") != "" {
return os.Getenv("SNAPPY_FORCE_SAS_URL")
if u := os.Getenv("SNAPPY_FORCE_SAS_URL"); u != "" {
return u
}

return "https://assertions.ubuntu.com/v1/"
Expand Down

0 comments on commit 5221eba

Please sign in to comment.