Skip to content

Commit

Permalink
Merge pull request #4 from doneill/feature/jdo-22-viper
Browse files Browse the repository at this point in the history
Init Viper write cofig
  • Loading branch information
doneill authored Nov 22, 2023
2 parents 8f2a240 + 6a2dae9 commit 799f00e
Show file tree
Hide file tree
Showing 5 changed files with 554 additions and 6 deletions.
6 changes: 3 additions & 3 deletions api/earthranger_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

// ----------------------------------------------
// static var
// const var
// ----------------------------------------------

var DOMAIN = ".pamdas.org"
var API_AUTH = "/oauth2/token"
const DOMAIN = ".pamdas.org"
const API_AUTH = "/oauth2/token"

// ----------------------------------------------
// stucts
Expand Down
12 changes: 10 additions & 2 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/doneill/er-cli-go/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// ----------------------------------------------
Expand Down Expand Up @@ -50,8 +51,15 @@ func auth() {

// Print out the access token and expires in if the request was successful
if response != nil {
fmt.Printf("Access Token: %s\n", response.AccessToken)
fmt.Printf("Expires In: %d\n", response.ExpiresIn)
viper.Set("user", USERNAME)
viper.Set("oauth_token", response.AccessToken)
viper.Set("expires", response.ExpiresIn)
err := viper.WriteConfigAs(PROGRAM_NAME + CONFIG_TYPE)
if err != nil {
fmt.Println("Error writing configuration file:", err)
} else {
fmt.Println("Authenticated!")
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// ----------------------------------------------
// const var
// ----------------------------------------------

const PROGRAM_NAME string = "er"
const CONFIG_TYPE string = ".toml"
const CONFIG_PATH string = "/Users/dano/.config/er/"

// ----------------------------------------------
// command
// ----------------------------------------------

var rootCmd = &cobra.Command{
Use: "er",
Short: "EarthRanger CLI",
Expand All @@ -20,6 +33,17 @@ func Execute() {
}
}

// ----------------------------------------------
// init
// ----------------------------------------------

func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
cobra.OnInitialize(initConfig)
}

func initConfig() {
viper.SetConfigName(PROGRAM_NAME)
viper.SetConfigType(CONFIG_TYPE)
viper.AddConfigPath(CONFIG_PATH)
}
23 changes: 22 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,30 @@ module github.com/doneill/er-cli-go

go 1.21.4

require github.com/spf13/cobra v1.8.0
require (
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 799f00e

Please sign in to comment.