Skip to content

Commit

Permalink
Merge pull request #298 from gabriel-samfira/add-format-option
Browse files Browse the repository at this point in the history
Add --format command line option
  • Loading branch information
gabriel-samfira authored Sep 28, 2024
2 parents cf231c9 + 6300011 commit 5323fcb
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/garm-cli/cmd/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

apiClientController "github.com/cloudbase/garm/client/controller"
apiClientControllerInfo "github.com/cloudbase/garm/client/controller_info"
"github.com/cloudbase/garm/cmd/garm-cli/common"
"github.com/cloudbase/garm/params"
)

Expand Down Expand Up @@ -164,6 +165,10 @@ func renderControllerInfoTable(info params.ControllerInfo) string {
}

func formatInfo(info params.ControllerInfo) error {
if outputFormat == common.OutputFormatJSON {
printAsJSON(info)
return nil
}
fmt.Println(renderControllerInfoTable(info))
return nil
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/garm-cli/cmd/enterprise.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/spf13/cobra"

apiClientEnterprises "github.com/cloudbase/garm/client/enterprises"
"github.com/cloudbase/garm/cmd/garm-cli/common"
"github.com/cloudbase/garm/params"
)

Expand Down Expand Up @@ -200,6 +201,10 @@ func init() {
}

func formatEnterprises(enterprises []params.Enterprise) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(enterprises)
return
}
t := table.NewWriter()
header := table.Row{"ID", "Name", "Endpoint", "Credentials name", "Pool Balancer Type", "Pool mgr running"}
t.AppendHeader(header)
Expand All @@ -211,6 +216,10 @@ func formatEnterprises(enterprises []params.Enterprise) {
}

func formatOneEnterprise(enterprise params.Enterprise) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(enterprise)
return
}
t := table.NewWriter()
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}
header := table.Row{"Field", "Value"}
Expand Down
9 changes: 9 additions & 0 deletions cmd/garm-cli/cmd/github_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/spf13/cobra"

apiClientCreds "github.com/cloudbase/garm/client/credentials"
"github.com/cloudbase/garm/cmd/garm-cli/common"
"github.com/cloudbase/garm/params"
)

Expand Down Expand Up @@ -342,6 +343,10 @@ func parseCredentialsUpdateParams() (params.UpdateGithubCredentialsParams, error
}

func formatGithubCredentials(creds []params.GithubCredentials) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(creds)
return
}
t := table.NewWriter()
header := table.Row{"ID", "Name", "Description", "Base URL", "API URL", "Upload URL", "Type"}
t.AppendHeader(header)
Expand All @@ -353,6 +358,10 @@ func formatGithubCredentials(creds []params.GithubCredentials) {
}

func formatOneGithubCredential(cred params.GithubCredentials) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(cred)
return
}
t := table.NewWriter()
header := table.Row{"Field", "Value"}
t.AppendHeader(header)
Expand Down
9 changes: 9 additions & 0 deletions cmd/garm-cli/cmd/github_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"

apiClientEndpoints "github.com/cloudbase/garm/client/endpoints"
"github.com/cloudbase/garm/cmd/garm-cli/common"
"github.com/cloudbase/garm/params"
)

Expand Down Expand Up @@ -250,6 +251,10 @@ func parseCreateParams() (params.CreateGithubEndpointParams, error) {
}

func formatEndpoints(endpoints params.GithubEndpoints) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(endpoints)
return
}
t := table.NewWriter()
header := table.Row{"Name", "Base URL", "Description"}
t.AppendHeader(header)
Expand All @@ -261,6 +266,10 @@ func formatEndpoints(endpoints params.GithubEndpoints) {
}

func formatOneEndpoint(endpoint params.GithubEndpoint) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(endpoint)
return
}
t := table.NewWriter()
header := table.Row{"Field", "Value"}
t.AppendHeader(header)
Expand Down
5 changes: 5 additions & 0 deletions cmd/garm-cli/cmd/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/spf13/cobra"

apiClientJobs "github.com/cloudbase/garm/client/jobs"
"github.com/cloudbase/garm/cmd/garm-cli/common"
"github.com/cloudbase/garm/params"
)

Expand Down Expand Up @@ -57,6 +58,10 @@ var jobsListCmd = &cobra.Command{
}

func formatJobs(jobs []params.Job) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(jobs)
return
}
t := table.NewWriter()
header := table.Row{"ID", "Name", "Status", "Conclusion", "Runner Name", "Repository", "Requested Labels", "Locked by"}
t.AppendHeader(header)
Expand Down
9 changes: 9 additions & 0 deletions cmd/garm-cli/cmd/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/cloudbase/garm-provider-common/util"
apiClientOrgs "github.com/cloudbase/garm/client/organizations"
"github.com/cloudbase/garm/cmd/garm-cli/common"
"github.com/cloudbase/garm/params"
)

Expand Down Expand Up @@ -340,6 +341,10 @@ func init() {
}

func formatOrganizations(orgs []params.Organization) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(orgs)
return
}
t := table.NewWriter()
header := table.Row{"ID", "Name", "Endpoint", "Credentials name", "Pool Balancer Type", "Pool mgr running"}
t.AppendHeader(header)
Expand All @@ -351,6 +356,10 @@ func formatOrganizations(orgs []params.Organization) {
}

func formatOneOrganization(org params.Organization) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(org)
return
}
t := table.NewWriter()
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}
header := table.Row{"Field", "Value"}
Expand Down
9 changes: 9 additions & 0 deletions cmd/garm-cli/cmd/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
apiClientOrgs "github.com/cloudbase/garm/client/organizations"
apiClientPools "github.com/cloudbase/garm/client/pools"
apiClientRepos "github.com/cloudbase/garm/client/repositories"
"github.com/cloudbase/garm/cmd/garm-cli/common"
"github.com/cloudbase/garm/params"
)

Expand Down Expand Up @@ -466,6 +467,10 @@ func asRawMessage(data []byte) (json.RawMessage, error) {
}

func formatPools(pools []params.Pool) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(pools)
return
}
t := table.NewWriter()
header := table.Row{"ID", "Image", "Flavor", "Tags", "Belongs to", "Level", "Enabled", "Runner Prefix", "Priority"}
t.AppendHeader(header)
Expand Down Expand Up @@ -496,6 +501,10 @@ func formatPools(pools []params.Pool) {
}

func formatOnePool(pool params.Pool) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(pool)
return
}
t := table.NewWriter()
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}

Expand Down
4 changes: 4 additions & 0 deletions cmd/garm-cli/cmd/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ func init() {
}

func formatProfiles(profiles []config.Manager) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(profiles)
return
}
t := table.NewWriter()
header := table.Row{"Name", "Base URL"}
t.AppendHeader(header)
Expand Down
5 changes: 5 additions & 0 deletions cmd/garm-cli/cmd/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/spf13/cobra"

apiClientProviders "github.com/cloudbase/garm/client/providers"
"github.com/cloudbase/garm/cmd/garm-cli/common"
"github.com/cloudbase/garm/params"
)

Expand Down Expand Up @@ -64,6 +65,10 @@ func init() {
}

func formatProviders(providers []params.Provider) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(providers)
return
}
t := table.NewWriter()
header := table.Row{"Name", "Description", "Type"}
t.AppendHeader(header)
Expand Down
9 changes: 9 additions & 0 deletions cmd/garm-cli/cmd/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/cloudbase/garm-provider-common/util"
apiClientRepos "github.com/cloudbase/garm/client/repositories"
"github.com/cloudbase/garm/cmd/garm-cli/common"
"github.com/cloudbase/garm/params"
)

Expand Down Expand Up @@ -346,6 +347,10 @@ func init() {
}

func formatRepositories(repos []params.Repository) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(repos)
return
}
t := table.NewWriter()
header := table.Row{"ID", "Owner", "Name", "Endpoint", "Credentials name", "Pool Balancer Type", "Pool mgr running"}
t.AppendHeader(header)
Expand All @@ -357,6 +362,10 @@ func formatRepositories(repos []params.Repository) {
}

func formatOneRepository(repo params.Repository) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(repo)
return
}
t := table.NewWriter()
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}
header := table.Row{"Field", "Value"}
Expand Down
17 changes: 17 additions & 0 deletions cmd/garm-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cmd

import (
"encoding/json"
"fmt"
"net/url"
"os"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/spf13/cobra"

apiClient "github.com/cloudbase/garm/client"
"github.com/cloudbase/garm/cmd/garm-cli/common"
"github.com/cloudbase/garm/cmd/garm-cli/config"
"github.com/cloudbase/garm/params"
)
Expand All @@ -37,6 +39,7 @@ var (
needsInit bool
debug bool
poolBalancerType string
outputFormat common.OutputFormat
errNeedsInitError = fmt.Errorf("please log into a garm installation first")
)

Expand All @@ -51,6 +54,11 @@ var rootCmd = &cobra.Command{
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "Enable debug on all API calls")
rootCmd.PersistentFlags().VarP(&outputFormat, "format", "f", "Output format (table, json)")
if outputFormat.String() == "" {
outputFormat = common.OutputFormatTable
}

cobra.OnInitialize(initConfig)

err := rootCmd.Execute()
Expand Down Expand Up @@ -113,3 +121,12 @@ func formatOneHookInfo(hook params.HookInfo) {
})
fmt.Println(t.Render())
}

func printAsJSON(value interface{}) {
asJs, err := json.Marshal(value)
if err != nil {
fmt.Printf("Failed to marshal value to json: %s", err)
os.Exit(1)
}
fmt.Println(string(asJs))
}
9 changes: 9 additions & 0 deletions cmd/garm-cli/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
apiClientInstances "github.com/cloudbase/garm/client/instances"
apiClientOrgs "github.com/cloudbase/garm/client/organizations"
apiClientRepos "github.com/cloudbase/garm/client/repositories"
"github.com/cloudbase/garm/cmd/garm-cli/common"
"github.com/cloudbase/garm/params"
)

Expand Down Expand Up @@ -222,6 +223,10 @@ func init() {
}

func formatInstances(param []params.Instance, detailed bool) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(param)
return
}
t := table.NewWriter()
header := table.Row{"Nr", "Name", "Status", "Runner Status", "Pool ID"}
if detailed {
Expand All @@ -242,6 +247,10 @@ func formatInstances(param []params.Instance, detailed bool) {
}

func formatSingleInstance(instance params.Instance) {
if outputFormat == common.OutputFormatJSON {
printAsJSON(instance)
return
}
t := table.NewWriter()

header := table.Row{"Field", "Value"}
Expand Down
28 changes: 28 additions & 0 deletions cmd/garm-cli/common/cobra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package common

import "fmt"

type OutputFormat string

const (
OutputFormatTable OutputFormat = "table"
OutputFormatJSON OutputFormat = "json"
)

func (o OutputFormat) String() string {
return string(o)
}

func (o *OutputFormat) Set(value string) error {
switch value {
case "table", "json":
*o = OutputFormat(value)
default:
return fmt.Errorf("allowed formats are: json, table")
}
return nil
}

func (o OutputFormat) Type() string {
return "string"
}

0 comments on commit 5323fcb

Please sign in to comment.