Added access token envvar check and changed logging
This commit is contained in:
parent
e4c7338142
commit
bcf71fa297
1 changed files with 19 additions and 4 deletions
23
cmd/fetch.go
23
cmd/fetch.go
|
|
@ -6,9 +6,10 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/OpenCHAMI/configurator/pkg/util"
|
"github.com/OpenCHAMI/configurator/pkg/util"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -25,10 +26,24 @@ var fetchCmd = &cobra.Command{
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// make sure a host is set
|
// make sure a host is set
|
||||||
if remoteHost == "" {
|
if remoteHost == "" {
|
||||||
logrus.Errorf("no '--host' argument set")
|
log.Error().Msg("no '--host' argument set")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check to see if an access token is available from env
|
||||||
|
if config.AccessToken == "" {
|
||||||
|
// check if OCHAMI_ACCESS_TOKEN env var is set if no access token is provided and use that instead
|
||||||
|
accessToken := os.Getenv("ACCESS_TOKEN")
|
||||||
|
if accessToken != "" {
|
||||||
|
config.AccessToken = accessToken
|
||||||
|
} else {
|
||||||
|
// TODO: try and fetch token first if it is needed
|
||||||
|
if verbose {
|
||||||
|
fmt.Printf("No token found. Attempting to generate config without one...\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// add the "Authorization" header if an access token is supplied
|
// add the "Authorization" header if an access token is supplied
|
||||||
headers := map[string]string{}
|
headers := map[string]string{}
|
||||||
if accessToken != "" {
|
if accessToken != "" {
|
||||||
|
|
@ -40,13 +55,13 @@ var fetchCmd = &cobra.Command{
|
||||||
url := fmt.Sprintf("%s:%d/generate?target=%s", remoteHost, remotePort, target)
|
url := fmt.Sprintf("%s:%d/generate?target=%s", remoteHost, remotePort, target)
|
||||||
res, body, err := util.MakeRequest(url, http.MethodGet, nil, headers)
|
res, body, err := util.MakeRequest(url, http.MethodGet, nil, headers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("failed to make request: %v", err)
|
log.Error().Err(err).Msg("failed to make request")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// handle getting other error codes other than a 200
|
// handle getting other error codes other than a 200
|
||||||
if res != nil {
|
if res != nil {
|
||||||
if res.StatusCode == http.StatusOK {
|
if res.StatusCode == http.StatusOK {
|
||||||
fmt.Printf("%s\n", string(body))
|
log.Info().Msgf("%s\n", string(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue