~ubuntu-branches/ubuntu/saucy/juju-core/saucy

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/cmd/juju/environmentcommand.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 16:02:16 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20130820160216-5yu1llasa2e2youn
Tags: 1.13.1-0ubuntu1
* New upstream release.
  - Build and install juju metadata plugin.
  - d/NEWS: Add some guidance on upgrading environments from 1.11.x
    to 1.13.x.
* d/NEWS: Add details about lack of upgrade path from juju < 1.11
  and how to interact with older juju environments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2013 Canonical Ltd.
2
 
// Licensed under the AGPLv3, see LICENCE file for details.
3
 
 
4
 
package main
5
 
 
6
 
import (
7
 
        "fmt"
8
 
        "io/ioutil"
9
 
        "os"
10
 
        "path/filepath"
11
 
        "strings"
12
 
 
13
 
        "launchpad.net/gnuflag"
14
 
        "launchpad.net/juju-core/cmd"
15
 
        "launchpad.net/juju-core/environs/config"
16
 
)
17
 
 
18
 
const CurrentEnvironmentFilename = "current-environment"
19
 
 
20
 
// The purpose of EnvCommandBase is to provide a default member and flag
21
 
// setting for commands that deal across different environments.
22
 
type EnvCommandBase struct {
23
 
        cmd.CommandBase
24
 
        EnvName string
25
 
}
26
 
 
27
 
func getCurrentEnvironmentFilePath() string {
28
 
        return filepath.Join(config.JujuHome(), CurrentEnvironmentFilename)
29
 
}
30
 
 
31
 
// Read the file $JUJU_HOME/current-environment and return the value stored
32
 
// there.  If the file doesn't exist, or there is a problem reading the file,
33
 
// an empty string is returned.
34
 
func readCurrentEnvironment() string {
35
 
        current, err := ioutil.ReadFile(getCurrentEnvironmentFilePath())
36
 
        // The file not being there, or not readable isn't really an error for us
37
 
        // here.  We treat it as "can't tell, so you get the default".
38
 
        if err != nil {
39
 
                return ""
40
 
        }
41
 
        return strings.TrimSpace(string(current))
42
 
}
43
 
 
44
 
// Write the envName to the file $JUJU_HOME/current-environment file.
45
 
func writeCurrentEnvironment(envName string) error {
46
 
        path := getCurrentEnvironmentFilePath()
47
 
        err := ioutil.WriteFile(path, []byte(envName+"\n"), 0644)
48
 
        if err != nil {
49
 
                return fmt.Errorf("unable to write to the environment file: %q, %s", path, err)
50
 
        }
51
 
        return nil
52
 
}
53
 
 
54
 
// There is simple ordering for the default environment.  Firstly check the
55
 
// JUJU_ENV environment variable.  If that is set, it gets used.  If it isn't
56
 
// set, look in the $JUJU_HOME/current-environment file.
57
 
func getDefaultEnvironment() string {
58
 
        defaultEnv := os.Getenv("JUJU_ENV")
59
 
        if defaultEnv != "" {
60
 
                return defaultEnv
61
 
        }
62
 
        return readCurrentEnvironment()
63
 
}
64
 
 
65
 
func (c *EnvCommandBase) SetFlags(f *gnuflag.FlagSet) {
66
 
        defaultEnv := getDefaultEnvironment()
67
 
        f.StringVar(&c.EnvName, "e", defaultEnv, "juju environment to operate in")
68
 
        f.StringVar(&c.EnvName, "environment", defaultEnv, "")
69
 
}