Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reads Hub API Server URL from '.tekton/hub-config' if present #260

Merged
merged 1 commit into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}