Skip to content

Commit

Permalink
new help formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Svihla committed Mar 3, 2021
1 parent 7e37805 commit 69e5e7e
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 51 deletions.
4 changes: 2 additions & 2 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func DBUsage() string {

// ExecuteDB launches several different subcommands and as of today is the main entry point
// into automation of Astra
func ExecuteDB(args []string, confFile string) error {
func ExecuteDB(args []string, confFile string, verbose bool) error {
clientInfo, err := pkg.ReadLogin(confFile)
if err != nil {
return fmt.Errorf("%v", err)
}
client, err := astraops.Authenticate(clientInfo.ClientName, clientInfo.ClientID, clientInfo.ClientSecret)
client, err := astraops.Authenticate(clientInfo, verbose)
if err != nil {
return fmt.Errorf("authenticate failed with error %v", err)
}
Expand Down
8 changes: 1 addition & 7 deletions cmd/db/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package db
import (
"flag"
"fmt"
"strings"

"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"
Expand All @@ -36,12 +35,7 @@ var createDbCloudProviderFlag = createCmd.String("cloudProvider", "GCP", "cloud

// CreateUsage shows the help for the create command
func CreateUsage() string {
var out strings.Builder
out.WriteString("\tcreate\n")
createCmd.VisitAll(func(f *flag.Flag) {
out.WriteString(fmt.Sprintf("\t\t%v\n", f.Usage))
})
return out.String()
return pkg.PrintFlags(createCmd, "create", "creates a database by id")
}

// ExecuteCreate submits a new database to astra
Expand Down
2 changes: 1 addition & 1 deletion cmd/db/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// DeleteUsage shows the help for the delete command
func DeleteUsage() string {
return "\tdelete <id>\n\t\tdeletes a database by id\n"
return "\tdelete <id> #deletes a database by id\n"
}

// ExecuteDelete removes the database with the specified ID. If no ID is provided
Expand Down
7 changes: 1 addition & 6 deletions cmd/db/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ var getFmt = getCmd.String("format", "text", "Output format for report default i

// GetUsage shows the help for the get command
func GetUsage() string {
var out strings.Builder
out.WriteString("\tget <id>\n")
getCmd.VisitAll(func(f *flag.Flag) {
out.WriteString(fmt.Sprintf("\t\t%v\n", f.Usage))
})
return out.String()
return pkg.PrintFlags(getCmd, "get <id>", "get database by id")
}

// ExecuteGet get the database with the specified ID. If no ID is provided
Expand Down
7 changes: 1 addition & 6 deletions cmd/db/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ var listFmt = listCmd.String("format", "text", "Output format for report default

// ListUsage shows the help for the List command
func ListUsage() string {
var out strings.Builder
out.WriteString("\tlist\n")
listCmd.VisitAll(func(f *flag.Flag) {
out.WriteString(fmt.Sprintf("\t\t%v\n", f.Usage))
})
return out.String()
return pkg.PrintFlags(listCmd, "list", "lists all databases in account")
}

// ExecuteList lists databases in astra
Expand Down
2 changes: 1 addition & 1 deletion cmd/db/park.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// ParkUsage shows the help for the park command
func ParkUsage() string {
return "\tpark <id>\n\t\tparks a database by id\n"
return "\tpark <id> #parks a database by id\n"
}

// ExecutePark parks the database with the specified ID. If no ID is provided
Expand Down
2 changes: 1 addition & 1 deletion cmd/db/resize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

// ResizeUsage shows the help for the delete command
func ResizeUsage() string {
return "\tresize <id> <capacity unit>\n\t\tresizes a database by id with the specified capacity unit\n"
return "\tresize <id> <capacity unit> #resizes a database by id with the specified capacity unit\n"
}

// ExecuteResize resizes the database with the specified ID with the specified size. If no ID is provided
Expand Down
7 changes: 1 addition & 6 deletions cmd/db/tiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ var tiersFmt = tiersCmd.String("format", "text", "Output format for report defau

// TiersUsage shows the help for the Tiers command
func TiersUsage() string {
var out strings.Builder
out.WriteString("\ttiers\n")
tiersCmd.VisitAll(func(f *flag.Flag) {
out.WriteString(fmt.Sprintf("\t\t%v\n", f.Usage))
})
return out.String()
return pkg.PrintFlags(tiersCmd, "tiers", "lists all tiers available")
}

// ExecuteTiers lists tiers available to this login in astra
Expand Down
2 changes: 1 addition & 1 deletion cmd/db/unpark.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// UnparkUsage shows the help for the unpark command
func UnparkUsage() string {
return "\tunpark <id>\n\t\tparks a database by id\n"
return "\tunpark <id> #parks a database by id\n"
}

// ExecuteUnpark unparks the database with the specified ID. If no ID is provided
Expand Down
8 changes: 1 addition & 7 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"
"os"
"strings"
)

var loginCmd = flag.NewFlagSet("login", flag.ExitOnError)
Expand All @@ -34,12 +33,7 @@ var clientJSONFlag = loginCmd.String("json", "", "copy the json for service acco

//LoginUsage returns the usage text for login
func LoginUsage() string {
var out strings.Builder
out.WriteString("\tastra-cli login\n")
loginCmd.VisitAll(func(f *flag.Flag) {
out.WriteString(fmt.Sprintf("\t\t%v\n", f.Usage))
})
return out.String()
return pkg.PrintFlags(loginCmd, "astra-cli login", "stores off login credentials for astra devops api")
}

//ExecuteLogin logs into Astra
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/rsds143/astra-cli

go 1.16

require github.com/rsds143/astra-devops-sdk-go v0.1.5
require github.com/rsds143/astra-devops-sdk-go v0.2.0
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
github.com/rsds143/astra-devops-sdk-go v0.1.4-0.20210222125150-d3960adebd40 h1:fgClCsf9TmGJGy03lxCCPPlc7dXqBzPI1ASAME4I6gM=
github.com/rsds143/astra-devops-sdk-go v0.1.4-0.20210222125150-d3960adebd40/go.mod h1:LQaUwm75Ydy/z71nl466Xv0yw8ib5b9L6laTFXbvtHU=
github.com/rsds143/astra-devops-sdk-go v0.1.4 h1:pcle2nixye+eIbq/FB0+Jy0cEJgz1CKjevPXyJI/M+A=
github.com/rsds143/astra-devops-sdk-go v0.1.4/go.mod h1:LQaUwm75Ydy/z71nl466Xv0yw8ib5b9L6laTFXbvtHU=
github.com/rsds143/astra-devops-sdk-go v0.1.5 h1:BwE7ApI5r9eMySK9fmOmWpE9vSWh6+nRKeBOCBsoHbE=
github.com/rsds143/astra-devops-sdk-go v0.1.5/go.mod h1:LQaUwm75Ydy/z71nl466Xv0yw8ib5b9L6laTFXbvtHU=
github.com/rsds143/astra-devops-sdk-go v0.2.0 h1:Oq5fWejjQ7pr3D7IeFrEO7hybvGHa++MdTMtvA+VEIU=
github.com/rsds143/astra-devops-sdk-go v0.2.0/go.mod h1:LQaUwm75Ydy/z71nl466Xv0yw8ib5b9L6laTFXbvtHU=
16 changes: 10 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,39 @@ package main

import (
"errors"
"flag"
"fmt"
"github.com/rsds143/astra-cli/cmd"
"github.com/rsds143/astra-cli/pkg"
"os"
)

var verbose = flag.Bool("v", false, "turns on verbose logging")

func usage() {
fmt.Println("usage: astra-cli <cmd>")
flag.Usage()
fmt.Println("commands:")
fmt.Println(cmd.LoginUsage())
fmt.Println(cmd.DBUsage())
}
func main() {
flag.Parse()
confDir, confFile, err := pkg.GetHome()
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(3)
}
if len(os.Args) == 1 {
if flag.NArg() == 1 {
usage()
os.Exit(1)
}
switch os.Args[1] {
switch flag.Arg(0) {
case "login":
err = cmd.ExecuteLogin(os.Args[2:], confDir, confFile)
err = cmd.ExecuteLogin(flag.Args()[1:], confDir, confFile)
case "db":
err = cmd.ExecuteDB(os.Args[2:], confFile)
err = cmd.ExecuteDB(flag.Args()[1:], confFile, *verbose)
default:
fmt.Printf("%q is not valid command.\n", os.Args[1])
fmt.Printf("%q is not valid command.\n", flag.Arg(1))
os.Exit(1)
}
var e *pkg.ParseError
Expand Down
48 changes: 48 additions & 0 deletions pkg/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
Copyright 2021 Ryan Svihla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pkg

import (
"flag"
"fmt"
"strings"
)

//PrintFlags outputs all of the flag parameters for a flagset
func PrintFlags(flagSet *flag.FlagSet, name string, desc string) string {
var out strings.Builder
out.WriteString(fmt.Sprintf("\t%v #%v\n", name, desc))
var flags [][]string
flagSet.VisitAll(func(f *flag.Flag) {
if f.Value.String() != "" {
flags = append(flags, []string{
fmt.Sprintf("-%v", f.Name),
f.Usage,
fmt.Sprintf("default: %v", f.Value),
})
} else {
flags = append(flags, []string{
fmt.Sprintf("-%v", f.Name),
f.Usage,
"",
})
}
})
for _, row := range PadColumns(flags) {
out.WriteString(fmt.Sprintf("\t\t %v\n", strings.Join(row, " ")))
}
return out.String()
}

0 comments on commit 69e5e7e

Please sign in to comment.