[cli] Allow switching API host based on config
This commit is contained in:
parent
9c04a7102b
commit
260a7fbcaa
|
@ -2,19 +2,30 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/ente-io/cli/utils/constants"
|
||||||
|
"github.com/spf13/viper"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
downloadHost = "https://files.ente.io/?fileID="
|
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 {
|
func (c *Client) DownloadFile(ctx context.Context, fileID int64, absolutePath string) error {
|
||||||
req := c.downloadClient.R().
|
req := c.downloadClient.R().
|
||||||
SetContext(ctx).
|
SetContext(ctx).
|
||||||
SetOutput(absolutePath)
|
SetOutput(absolutePath)
|
||||||
attachToken(req)
|
attachToken(req)
|
||||||
r, err := req.Get(downloadHost + strconv.FormatInt(fileID, 10))
|
r, err := req.Get(downloadUrl(fileID))
|
||||||
if r.IsError() {
|
if r.IsError() {
|
||||||
return &ApiError{
|
return &ApiError{
|
||||||
StatusCode: r.StatusCode(),
|
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"
|
||||||
"github.com/ente-io/cli/pkg/secrets"
|
"github.com/ente-io/cli/pkg/secrets"
|
||||||
"github.com/ente-io/cli/utils/constants"
|
"github.com/ente-io/cli/utils/constants"
|
||||||
|
"github.com/spf13/viper"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"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)
|
log.Fatalf("Please mount a volume to %s to persist cli data\n%v\n", cliDBPath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not create cli config path\n%v\n", err)
|
log.Fatalf("Could not create cli config path\n%v\n", err)
|
||||||
}
|
}
|
||||||
|
initConfig(cliDBPath)
|
||||||
newCliPath := fmt.Sprintf("%s/ente-cli.db", cliDBPath)
|
newCliPath := fmt.Sprintf("%s/ente-cli.db", cliDBPath)
|
||||||
if !strings.HasPrefix(cliDBPath, "/") {
|
if !strings.HasPrefix(cliDBPath, "/") {
|
||||||
oldCliPath := fmt.Sprintf("%sente-cli.db", cliDBPath)
|
oldCliPath := fmt.Sprintf("%sente-cli.db", cliDBPath)
|
||||||
|
@ -48,8 +49,8 @@ func main() {
|
||||||
}
|
}
|
||||||
ctrl := pkg.ClICtrl{
|
ctrl := pkg.ClICtrl{
|
||||||
Client: api.NewClient(api.Params{
|
Client: api.NewClient(api.Params{
|
||||||
Debug: false,
|
Debug: viper.GetBool("log.http"),
|
||||||
//Host: "http://localhost:8080",
|
Host: viper.GetString("endpoint.api"),
|
||||||
}),
|
}),
|
||||||
DB: db,
|
DB: db,
|
||||||
KeyHolder: secrets.NewKeyHolder(secrets.GetOrCreateClISecret()),
|
KeyHolder: secrets.NewKeyHolder(secrets.GetOrCreateClISecret()),
|
||||||
|
@ -66,6 +67,23 @@ func main() {
|
||||||
cmd.Execute(&ctrl)
|
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.
|
// GetCLIConfigPath returns the path to the .ente-cli folder and creates it if it doesn't exist.
|
||||||
func GetCLIConfigPath() (string, error) {
|
func GetCLIConfigPath() (string, error) {
|
||||||
if os.Getenv("ENTE_CLI_CONFIG_PATH") != "" {
|
if os.Getenv("ENTE_CLI_CONFIG_PATH") != "" {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
package constants
|
package constants
|
||||||
|
|
||||||
const CliDataPath = "/cli-data/"
|
const CliDataPath = "/cli-data/"
|
||||||
|
const EnteApiUrl = "https://api.ente.io"
|
||||||
|
|
Loading…
Reference in a new issue