Skip to content

Commit

Permalink
Reads Hub API Server URL from '.tekton/hub-config'
Browse files Browse the repository at this point in the history
This adds support for reading `HUB_API_SERVER` URL from a file
at location $HOME/.tekton/hub-config.
If url is not flag is not as passed, it will check in hub-config file,
if still not defined then it will use default url.
URL passed by flag will get priority over URL in hub-config and default
URL.

Signed-off-by: Shivam Mukhade <[email protected]>
  • Loading branch information
SM43 authored and tekton-robot committed Jul 7, 2021
1 parent 352d4e5 commit 0ae1afc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
3 changes: 1 addition & 2 deletions api/pkg/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/tektoncd/hub/api/pkg/cli/cmd/reinstall"
"github.com/tektoncd/hub/api/pkg/cli/cmd/search"
"github.com/tektoncd/hub/api/pkg/cli/cmd/upgrade"
"github.com/tektoncd/hub/api/pkg/cli/hub"
)

// Root represents the base command when called without any subcommands
Expand Down Expand Up @@ -59,7 +58,7 @@ func Root(cli app.CLI) *cobra.Command {
check_upgrade.Command(cli),
)

cmd.PersistentFlags().StringVar(&apiURL, "api-server", hub.URL(), "Hub API Server URL")
cmd.PersistentFlags().StringVar(&apiURL, "api-server", "", "Hub API Server URL (default 'https://api.hub.tekton.dev').\nURL can also be defined in a file '$HOME/.tekton/hub-config' with a variable 'HUB_API_SERVER'.")

return cmd
}
53 changes: 50 additions & 3 deletions api/pkg/cli/hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os/user"

"github.com/joho/godotenv"
"github.com/spf13/viper"
)

const (
// hubURL - Hub API Server URL
hubURL = "https://api.hub.tekton.dev"
hubConfigPath = ".tekton/hub-config"
)

type Client interface {
Expand Down Expand Up @@ -53,14 +58,32 @@ func URL() string {
}

// SetURL validates and sets the hub apiURL server URL
// URL passed through flag will take precedence over the hub API URL
// in config file and default URL
func (h *client) SetURL(apiURL string) error {

_, err := url.ParseRequestURI(apiURL)
if err != nil {
if apiURL != "" {
_, err := url.ParseRequestURI(apiURL)
if err != nil {
return err
}
h.apiURL = apiURL
return nil
}

if err := loadConfigFile(); err != nil {
return err
}

h.apiURL = apiURL
viper.AutomaticEnv()
if apiURL := viper.GetString("HUB_API_SERVER"); apiURL != "" {
_, err := url.ParseRequestURI(apiURL)
if err != nil {
return fmt.Errorf("invalid url set for HUB_API_SERVER: %s : %v", apiURL, err)
}
h.apiURL = apiURL
}

return nil
}

Expand All @@ -87,6 +110,12 @@ func (h *client) Get(endpoint string) ([]byte, int, error) {

// httpGet gets raw data given the url
func httpGet(url string) ([]byte, int, error) {

err := loadConfigFile()
if err != nil {
return nil, 0, err
}

resp, err := http.Get(url)
if err != nil {
return nil, 0, err
Expand All @@ -100,3 +129,21 @@ func httpGet(url string) ([]byte, int, error) {

return data, resp.StatusCode, err
}

// Looks for config file at $HOME/.tekton/hub-config and loads into
// in the environment
func loadConfigFile() error {

user, err := user.Current()
if err != nil {
return err
}

// if hub-config file not found, then returns
path := fmt.Sprintf("%s/%s", user.HomeDir, hubConfigPath)
if err := godotenv.Load(path); err != nil {
return nil
}

return nil
}

0 comments on commit 0ae1afc

Please sign in to comment.