Skip to content

Commit

Permalink
Merge pull request #1 from doneill/feature/jdo-17-initialize-cobra
Browse files Browse the repository at this point in the history
Initialize cobra cli framework
  • Loading branch information
doneill authored Nov 20, 2023
2 parents 6ba11ef + 64bbf25 commit 6f6696c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 52 deletions.
22 changes: 20 additions & 2 deletions api.go → cmd/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package cmd

import (
"encoding/json"
Expand All @@ -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{}
Expand Down
72 changes: 72 additions & 0 deletions cmd/auth.go
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)
}
25 changes: 25 additions & 0 deletions cmd/root.go
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")
}
6 changes: 6 additions & 0 deletions go.mod
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
)
10 changes: 10 additions & 0 deletions go.sum
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=
52 changes: 2 additions & 50 deletions main.go
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()
}

0 comments on commit 6f6696c

Please sign in to comment.