Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init Viper write cofig #4

Merged
merged 5 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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