[cli] Allow switching API host based on config
This commit is contained in:
parent
9c04a7102b
commit
260a7fbcaa
3 changed files with 34 additions and 4 deletions
|
@ -2,19 +2,30 @@ package api
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/ente-io/cli/utils/constants"
|
||||
"github.com/spf13/viper"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
downloadHost = "https://files.ente.io/?fileID="
|
||||
)
|
||||
|
||||
func downloadUrl(fileID int64) string {
|
||||
apiEndpoint := viper.GetString("endpoint.api")
|
||||
if apiEndpoint == "" || strings.Compare(apiEndpoint, constants.EnteApiUrl) == 0 {
|
||||
return downloadHost + strconv.FormatInt(fileID, 10)
|
||||
}
|
||||
return apiEndpoint + "/files/download/" + strconv.FormatInt(fileID, 10)
|
||||
}
|
||||
|
||||
func (c *Client) DownloadFile(ctx context.Context, fileID int64, absolutePath string) error {
|
||||
req := c.downloadClient.R().
|
||||
SetContext(ctx).
|
||||
SetOutput(absolutePath)
|
||||
attachToken(req)
|
||||
r, err := req.Get(downloadHost + strconv.FormatInt(fileID, 10))
|
||||
r, err := req.Get(downloadUrl(fileID))
|
||||
if r.IsError() {
|
||||
return &ApiError{
|
||||
StatusCode: r.StatusCode(),
|
||||
|
|
24
cli/main.go
24
cli/main.go
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/ente-io/cli/pkg"
|
||||
"github.com/ente-io/cli/pkg/secrets"
|
||||
"github.com/ente-io/cli/utils/constants"
|
||||
"github.com/spf13/viper"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -23,10 +24,10 @@ func main() {
|
|||
log.Fatalf("Please mount a volume to %s to persist cli data\n%v\n", cliDBPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Could not create cli config path\n%v\n", err)
|
||||
}
|
||||
initConfig(cliDBPath)
|
||||
newCliPath := fmt.Sprintf("%s/ente-cli.db", cliDBPath)
|
||||
if !strings.HasPrefix(cliDBPath, "/") {
|
||||
oldCliPath := fmt.Sprintf("%sente-cli.db", cliDBPath)
|
||||
|
@ -48,8 +49,8 @@ func main() {
|
|||
}
|
||||
ctrl := pkg.ClICtrl{
|
||||
Client: api.NewClient(api.Params{
|
||||
Debug: false,
|
||||
//Host: "http://localhost:8080",
|
||||
Debug: viper.GetBool("log.http"),
|
||||
Host: viper.GetString("endpoint.api"),
|
||||
}),
|
||||
DB: db,
|
||||
KeyHolder: secrets.NewKeyHolder(secrets.GetOrCreateClISecret()),
|
||||
|
@ -66,6 +67,23 @@ func main() {
|
|||
cmd.Execute(&ctrl)
|
||||
}
|
||||
|
||||
func initConfig(cliConfigPath string) {
|
||||
viper.SetConfigName("config") // name of config file (without extension)
|
||||
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
|
||||
viper.AddConfigPath(cliConfigPath + "/") // path to look for the config file in
|
||||
viper.AddConfigPath(".") // optionally look for config in the working directory
|
||||
|
||||
viper.SetDefault("endpoint.api", constants.EnteApiUrl)
|
||||
viper.SetDefault("log.http", false)
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
log.Printf("Config file not found; using defaults %s", cliConfigPath)
|
||||
} else {
|
||||
// Config file was found but another error was produced
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetCLIConfigPath returns the path to the .ente-cli folder and creates it if it doesn't exist.
|
||||
func GetCLIConfigPath() (string, error) {
|
||||
if os.Getenv("ENTE_CLI_CONFIG_PATH") != "" {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
package constants
|
||||
|
||||
const CliDataPath = "/cli-data/"
|
||||
const EnteApiUrl = "https://api.ente.io"
|
||||
|
|
Loading…
Add table
Reference in a new issue