Skip to content

Commit

Permalink
Merge pull request #13 from gridscale/release/release-v0.2.0-beta
Browse files Browse the repository at this point in the history
Release v0.2.0 beta
  • Loading branch information
bkircher authored Mar 17, 2020
2 parents c77bcc3 + f304b12 commit ab92319
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
gscloud
.DS_Store
build
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Changelog
## v0.2.0-beta (March 11, 2020)

FEATURES:
* Use standard user-level cache directory https://github.com/gridscale/gscloud/issues/11


## v0.1.0-beta (January 8, 2020)
- Initial release of gscloud

FEATURES:
* Support make-config for creating a new configuration file
* Support kubernetes cluster sub-commands: save-kubeconfig and exec-credential for managing authentication for cluster
* Support kubernetes cluster sub-commands: save-kubeconfig and exec-credential for managing a cluster's authentication
22 changes: 22 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Contributing to gscloud

As an open source project, we encourage and appreciate any kind of contributions like comments,
enhancement requests, bug reports, or code contributions!

## Development workflow

* Development is done on the `develop` branch.
* Releases are cut from the `master` branch.
* For anything larger than single commits, please use feature/bugfix branches.

## Reporting Bugs or Enhancement Requests

Please submit bugs or enhancement requests through the public bug tracker within the
GitHub project.

## Submitting Patches

The gscloud source code is managed using the git distributed source control
management tool <https://www.git-scm.org/>. Please submit patches
accordingly.

26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
PLATFORMS=windows linux darwin
ARCHES=amd64
BUILDDIR=build
VERSION=$$(cat VERSION)
EXECUTABLE_NAME=gscloud_$(VERSION)

buildall: clean build zip

build:
$(foreach platform,$(PLATFORMS), \
$(foreach arch,$(ARCHES), \
mkdir -p $(BUILDDIR); GOOS=$(platform) GOARCH=$(arch) go build -o $(BUILDDIR)/$(EXECUTABLE_NAME)_$(platform)_$(arch);))
@echo "Renaming Windows file"
@if [ -f $(BUILDDIR)/$(EXECUTABLE_NAME)_windows_$(ARCHES) ]; then mv $(BUILDDIR)/$(EXECUTABLE_NAME)_windows_$(ARCHES) $(BUILDDIR)/$(EXECUTABLE_NAME)_windows_$(ARCHES).exe; fi

zip:
$(foreach file,$(wildcard $(BUILDDIR)/*),\
zip -j $(file).zip $(file);)
@if [ -f $(BUILDDIR)/$(EXECUTABLE_NAME)_windows_$(ARCHES).exe.zip ]; then mv $(BUILDDIR)/$(EXECUTABLE_NAME)_windows_$(ARCHES).exe.zip $(BUILDDIR)/$(EXECUTABLE_NAME)_windows_$(ARCHES).zip; fi

clean:
$(foreach platform,$(PLATFORMS), \
$(foreach arch,$(ARCHES), \
rm -f $(BUILDDIR)/gscloud_*;))

.PHONY: buildall build clean zip
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# gscloud: The Command Line Interface for the gridcsale cloud
# gscloud: The Command Line Interface for the gridscale cloud

## Supported Services

Expand All @@ -20,7 +20,7 @@ Available Commands:
Flags:
--account string the account used, 'default' if none given
--config string configuration file, default /home/bk/.config/gridscale/config.yaml
--config string configuration file, default /home/bk/.config/gscloud/config.yaml
-h, --help help for gscloud
Use "gscloud [command] --help" for more information about a command.
Expand Down Expand Up @@ -69,7 +69,7 @@ users:
command: $HOME/gscloud
args:
- "--config"
- "$HOME/.gscloud/config.yaml"
- "$HOME/.config/gscloud/config.yaml"
- "--account"
- "test"
- "kubernetes"
Expand Down
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.2.0-beta
36 changes: 31 additions & 5 deletions cmd/cliconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"

"github.com/kardianos/osext"
"github.com/kirsle/configdir"
"github.com/spf13/viper"
)

Expand All @@ -20,22 +21,39 @@ type cliConfig struct {
}

func cliPath() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
filePath, err := osext.Executable()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
return filepath.Join(dir, os.Args[0])
return filePath
}

// configPath construct platform specific path to the configuration file.
// - on Linux: $XDG_CONFIG_HOME or $HOME/.config
// - on macOS: $HOME/Library/Application Support
// - on Windows: %APPDATA% or "C:\\Users\\%USER%\\AppData\\Roaming"
func cliConfigPath() string {
path := viper.ConfigFileUsed()
if path == "" {
path = configdir.LocalConfig("gscloud") + "/config.yaml"
viper.SetConfigFile(path)
}
return path
}

func cliCachePath() string {
return configdir.LocalCache("gscloud")
}

func newCliClient(account string) *gsclient {
var ac accountEntry

cliConf := &cliConfig{}
err := viper.Unmarshal(cliConf)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}

for _, a := range cliConf.Accounts {
if account == a.Name {
ac = a
Expand All @@ -52,3 +70,11 @@ func newCliClient(account string) *gsclient {
}
return newClient(clientConf)
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
14 changes: 3 additions & 11 deletions cmd/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var saveKubeconfigCmd = &cobra.Command{
Command: cliPath(),
Args: []string{
"--config",
cfgFile,
cliConfigPath(),
"--account",
account,
"kubernetes",
Expand All @@ -102,6 +102,7 @@ var saveKubeconfigCmd = &cobra.Command{
"--cluster",
clusterID,
},
Env: []clientcmdapi.ExecEnvVar{},
},
}
} else {
Expand Down Expand Up @@ -253,8 +254,7 @@ func fetchKubeConfigFromProvider(id string) *kubeConfig {
}

func kubeConfigCachePath() string {
dir, _ := filepath.Abs(filepath.Dir(cfgFile))
return filepath.Join(dir, "cache", "exec-credential")
return filepath.Join(cliCachePath(), "exec-credential")
}

func cachedKubeConfigPath(id string) string {
Expand Down Expand Up @@ -308,11 +308,3 @@ func loadCachedKubeConfig(id string) (*clientauth.ExecCredential, error) {

return execCredential, nil
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
29 changes: 13 additions & 16 deletions cmd/makeConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

Expand All @@ -29,25 +29,22 @@ Create a new configuration file at a specified path:
gscloud --config ~/myconfig.yaml make-config
`, configPath()),
`, cliConfigPath()),
RunE: func(cmd *cobra.Command, args []string) error {
path := viper.ConfigFileUsed()
if path == "" {
path = configPath() + "/config.yaml"
viper.SetConfigFile(path)
}
filePath := cliConfigPath()

err := os.MkdirAll(configPath(), 0755)
if err != nil {
return err
}
if !fileExists(filePath) {
err := os.MkdirAll(filepath.Dir(filePath), os.FileMode(0700))
if err != nil {
return err
}

err = ioutil.WriteFile(path, emptyConfig(), 0644)
if err != nil {
return err
err = ioutil.WriteFile(filePath, emptyConfig(), 0644)
if err != nil {
return err
}
}

fmt.Println(path)
fmt.Println(filePath)
return nil
},
}
Expand Down
16 changes: 4 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"

"github.com/kirsle/configdir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -33,7 +32,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", fmt.Sprintf("configuration file, default %s/config.yaml", configPath()))
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", fmt.Sprintf("configuration file, default %s", cliConfigPath()))
rootCmd.PersistentFlags().StringVar(&account, "account", "", "the account used, 'default' if none given")

rootCmd.AddCommand(kubernetesCmd)
Expand All @@ -42,14 +41,6 @@ func init() {

}

// configPath construct platform specific path to the configuration file.
// - on Linux: $XDG_CONFIG_HOME or $HOME/.config
// - on macOS: $HOME/Library/Application Support
// - on Windows: %APPDATA% or "C:\\Users\\%USER%\\AppData\\Roaming"
func configPath() string {
return configdir.LocalConfig("gridscale")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
Expand All @@ -59,7 +50,7 @@ func initConfig() {
// Use default paths.
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(configPath())
viper.AddConfigPath(cliConfigPath())
viper.AddConfigPath(".")
}
viper.AutomaticEnv() // read in environment variables that match
Expand All @@ -74,10 +65,11 @@ func initConfig() {
os.Exit(1)
}
}

if account == "" {
account = "default"
}

client = newCliClient(account)
if client == nil {
os.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.13

require (
github.com/imdario/mergo v0.3.7 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v0.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
Expand Down

0 comments on commit ab92319

Please sign in to comment.