-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from doneill/feature/jdo-17-initialize-cobra
Initialize cobra cli framework
- Loading branch information
Showing
6 changed files
with
135 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |