diff --git a/api.go b/cmd/api.go similarity index 75% rename from api.go rename to cmd/api.go index 62ce483..c8d6df5 100644 --- a/api.go +++ b/cmd/api.go @@ -1,4 +1,4 @@ -package main +package cmd import ( "encoding/json" @@ -9,11 +9,29 @@ import ( ) // ---------------------------------------------- -// static globals +// static var // ---------------------------------------------- + var DOMAIN = ".pamdas.org" var API_AUTH = "/oauth2/token" +// ---------------------------------------------- +// stucts +// ---------------------------------------------- + +type Response struct { + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` + TokenType string `json:"token_type"` + Scope string `json:"scope"` + RefreshToken string `json:"refresh_token"` + ErrorDescription string `json:"error_description"` +} + +// ---------------------------------------------- +// public funtions +// ---------------------------------------------- + func authenticate(sitename, username, password string) (*Response, error) { // Create a new HTTP client and make a POST request to the authentication endpoint client := &http.Client{} diff --git a/cmd/auth.go b/cmd/auth.go new file mode 100644 index 0000000..78be616 --- /dev/null +++ b/cmd/auth.go @@ -0,0 +1,72 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +// ---------------------------------------------- +// auth command +// ---------------------------------------------- + +var authCmd = &cobra.Command{ + Use: "auth", + Short: "Authentication with EarthRanger", + Long: `Authenticate er with EarthRanger`, + Run: func(cmd *cobra.Command, args []string) { + auth() + }, +} + +// ---------------------------------------------- +// funtions +// ---------------------------------------------- + +func auth() { + fmt.Println("Enter sitename:") + var sitename string + _, err := fmt.Scan(&sitename) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + fmt.Println("Enter username:") + var username string + _, err = fmt.Scan(&username) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + fmt.Println("Enter password:") + var password string + _, err = fmt.Scan(&password) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // Call the authenticate function to get the access token and expires in + response, err := authenticate(sitename, username, password) + if err != nil { + fmt.Println("Error authenticating:", err) + os.Exit(1) + } + + // 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) + } +} + +// ---------------------------------------------- +// initialize +// ---------------------------------------------- + +func init() { + rootCmd.AddCommand(authCmd) +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..032069c --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "er", + Short: "EarthRanger CLI", + Long: `Work with EarthRanger platform from command line`, + Version: "0.1.0", +} + +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/go.mod b/go.mod index d286bfc..cd55ce5 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module github.com/doneill/er-cli-go go 1.21.4 + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/cobra v1.8.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d0e8c2c --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= +github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 0d8612b..5ca5ea0 100644 --- a/main.go +++ b/main.go @@ -1,55 +1,7 @@ package main -import ( - "fmt" - "os" -) - -type Response struct { - AccessToken string `json:"access_token"` - ExpiresIn int `json:"expires_in"` - TokenType string `json:"token_type"` - Scope string `json:"scope"` - RefreshToken string `json:"refresh_token"` - ErrorDescription string `json:"error_description"` -} +import "github.com/doneill/er-cli-go/cmd" func main() { - // Get the sitename, username, and password from CLI arguments - fmt.Println("Enter sitename:") - var sitename string - _, err := fmt.Scan(&sitename) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - fmt.Println("Enter username:") - var username string - _, err = fmt.Scan(&username) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - fmt.Println("Enter password:") - var password string - _, err = fmt.Scan(&password) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - // Call the authenticate function to get the access token and expires in - response, err := authenticate(sitename, username, password) - if err != nil { - fmt.Println("Error authenticating:", err) - os.Exit(1) - } - - // 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) - } + cmd.Execute() }