mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 03:27:02 -07:00
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 (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/OpenCHAMI/configurator/pkg/util"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
|
@ -25,10 +26,24 @@ var fetchCmd = &cobra.Command{
|
|||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// make sure a host is set
|
||||
if remoteHost == "" {
|
||||
logrus.Errorf("no '--host' argument set")
|
||||
log.Error().Msg("no '--host' argument set")
|
||||
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
|
||||
headers := map[string]string{}
|
||||
if accessToken != "" {
|
||||
|
|
@ -40,13 +55,13 @@ var fetchCmd = &cobra.Command{
|
|||
url := fmt.Sprintf("%s:%d/generate?target=%s", remoteHost, remotePort, target)
|
||||
res, body, err := util.MakeRequest(url, http.MethodGet, nil, headers)
|
||||
if err != nil {
|
||||
logrus.Errorf("failed to make request: %v", err)
|
||||
log.Error().Err(err).Msg("failed to make request")
|
||||
return
|
||||
}
|
||||
// handle getting other error codes other than a 200
|
||||
if res != nil {
|
||||
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