Skip to content

Commit

Permalink
Rename host to endpoint and include /graphql as default
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Scott committed Jun 5, 2018
1 parent 31b93b4 commit 4486a42
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
17 changes: 8 additions & 9 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (
)

type Client struct {
host string
token string
client *graphql.Client
logger *logger.Logger
endpoint string
token string
client *graphql.Client
logger *logger.Logger
}

func NewClient(host string, token string, logger *logger.Logger) *Client {
func NewClient(endpoint string, token string, logger *logger.Logger) *Client {
return &Client{
host,
endpoint,
token,
graphql.NewClient(host + "/graphql"),
graphql.NewClient(endpoint),
logger,
}
}
Expand All @@ -30,8 +30,7 @@ func (c *Client) Run(query string) (map[string]interface{}, error) {
ctx := context.Background()
var resp map[string]interface{}

// TODO: fix wrapping logs with
c.logger.Info("Querying ", c.host, " with:\n\n", query, "\n\n")
c.logger.Info("Querying ", c.endpoint, " with:\n\n", query, "\n\n")
err := c.client.Run(ctx, req, &resp)
return resp, err
}
4 changes: 2 additions & 2 deletions cmd/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ var diagnosticCmd = &cobra.Command{
}

func diagnostic(cmd *cobra.Command, args []string) {
host := viper.GetString("host")
endpoint := viper.GetString("endpoint")
token := viper.GetString("token")

Logger.Info("\n---\nCircleCI CLI Diagnostics\n---\n\n")
Logger.Infof("Config found: `%v`\n", viper.ConfigFileUsed())

Logger.Infof("Host is: %s\n", host)
Logger.Infof("GraphQL API endpoint: %s\n", endpoint)

if token == "token" || token == "" {
Logger.Info("Please set a token!")
Expand Down
2 changes: 1 addition & 1 deletion cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var queryCmd = &cobra.Command{
}

func query(cmd *cobra.Command, args []string) {
client := client.NewClient(viper.GetString("host"), viper.GetString("token"), Logger)
client := client.NewClient(viper.GetString("endpoint"), viper.GetString("token"), Logger)

query, err := ioutil.ReadAll(os.Stdin)
Logger.FatalOnError("Something happened", err)
Expand Down
16 changes: 8 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging.")
Logger = logger.NewLogger(verbose)
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.circleci/cli.yml)")
rootCmd.PersistentFlags().StringP("host", "H", "https://circleci.com", "the host of your CircleCI install")
rootCmd.PersistentFlags().StringP("endpoint", "e", "https://circleci.com/graphql", "the endpoint of your CircleCI GraphQL API")
rootCmd.PersistentFlags().StringP("token", "t", "", "your token for using CircleCI")

Logger.FatalOnError("Error binding host flag", viper.BindPFlag("host", rootCmd.PersistentFlags().Lookup("host")))
Logger.FatalOnError("Error binding endpoint flag", viper.BindPFlag("endpoint", rootCmd.PersistentFlags().Lookup("endpoint")))
Logger.FatalOnError("Error binding token flag", viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token")))
}

Expand Down Expand Up @@ -78,7 +78,7 @@ func readConfig() (err error) {
}

// read in environment variables that match
// set a prefix for config, i.e. CIRCLECI_CLI_HOST
// set a prefix for config, i.e. CIRCLECI_CLI_ENDPOINT
viper.SetEnvPrefix("circleci_cli")
viper.AutomaticEnv()

Expand Down Expand Up @@ -117,12 +117,12 @@ func createConfig() (err error) {
defer Logger.FatalOnError("Error closing config file", file.Close())

// read flag values
host := viper.GetString("host")
endpoint := viper.GetString("endpoint")
token := viper.GetString("token")

if host == "host" || host == "" {
Logger.Info("Please enter the HTTP(S) host of your CircleCI installation:")
fmt.Scanln(&host)
if endpoint == "endpoint" || endpoint == "" {
Logger.Info("Please enter the HTTP(S) endpoint to your CircleCI GraphQL API:")
fmt.Scanln(&endpoint)
Logger.Info("OK.\n")
}

Expand All @@ -133,7 +133,7 @@ func createConfig() (err error) {
}

// format input
configValues := fmt.Sprintf("host: %v\ntoken: %v\n", host, token)
configValues := fmt.Sprintf("endpoint: %v\ntoken: %v\n", endpoint, token)

// write new config values to file
if _, err = file.WriteString(configValues); err != nil {
Expand Down

0 comments on commit 4486a42

Please sign in to comment.