2023-09-12 10:15:41 +00:00
|
|
|
package main
|
|
|
|
|
2023-09-13 08:51:05 +00:00
|
|
|
import (
|
2023-10-18 12:22:41 +00:00
|
|
|
"fmt"
|
2023-10-21 09:26:13 +00:00
|
|
|
"github.com/ente-io/cli/cmd"
|
|
|
|
"github.com/ente-io/cli/internal"
|
|
|
|
"github.com/ente-io/cli/internal/api"
|
|
|
|
"github.com/ente-io/cli/pkg"
|
|
|
|
"github.com/ente-io/cli/pkg/secrets"
|
|
|
|
"github.com/ente-io/cli/utils/constants"
|
2024-03-08 11:49:59 +00:00
|
|
|
"github.com/spf13/viper"
|
2023-10-18 12:22:41 +00:00
|
|
|
"log"
|
2023-10-25 02:58:54 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-10-21 04:45:38 +00:00
|
|
|
"strings"
|
2023-09-13 08:51:05 +00:00
|
|
|
)
|
|
|
|
|
2024-03-29 05:38:45 +00:00
|
|
|
var AppVersion = "0.1.13"
|
2024-03-13 06:08:30 +00:00
|
|
|
|
2023-09-12 10:15:41 +00:00
|
|
|
func main() {
|
2023-10-25 02:58:54 +00:00
|
|
|
cliDBPath, err := GetCLIConfigPath()
|
2023-11-01 04:38:26 +00:00
|
|
|
if secrets.IsRunningInContainer() {
|
|
|
|
cliDBPath = constants.CliDataPath
|
|
|
|
_, err := internal.ValidateDirForWrite(cliDBPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Please mount a volume to %s to persist cli data\n%v\n", cliDBPath, err)
|
|
|
|
}
|
|
|
|
}
|
2023-10-25 02:58:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not create cli config path\n%v\n", err)
|
|
|
|
}
|
2024-03-08 11:49:59 +00:00
|
|
|
initConfig(cliDBPath)
|
2023-10-30 05:06:34 +00:00
|
|
|
newCliPath := fmt.Sprintf("%s/ente-cli.db", cliDBPath)
|
2023-12-01 15:15:22 +00:00
|
|
|
if !strings.HasPrefix(cliDBPath, "/") {
|
|
|
|
oldCliPath := fmt.Sprintf("%sente-cli.db", cliDBPath)
|
|
|
|
if _, err := os.Stat(oldCliPath); err == nil {
|
|
|
|
log.Printf("migrating old cli db from %s to %s\n", oldCliPath, newCliPath)
|
|
|
|
if err := os.Rename(oldCliPath, newCliPath); err != nil {
|
|
|
|
log.Fatalf("Could not rename old cli db\n%v\n", err)
|
|
|
|
}
|
2023-10-30 05:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
db, err := pkg.GetDB(newCliPath)
|
2023-10-25 02:58:54 +00:00
|
|
|
|
2023-09-13 08:51:05 +00:00
|
|
|
if err != nil {
|
2023-10-21 04:45:38 +00:00
|
|
|
if strings.Contains(err.Error(), "timeout") {
|
|
|
|
log.Fatalf("Please close all other instances of the cli and try again\n%v\n", err)
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-09-13 08:51:05 +00:00
|
|
|
}
|
|
|
|
ctrl := pkg.ClICtrl{
|
2023-09-15 10:55:29 +00:00
|
|
|
Client: api.NewClient(api.Params{
|
2024-03-08 11:49:59 +00:00
|
|
|
Debug: viper.GetBool("log.http"),
|
|
|
|
Host: viper.GetString("endpoint.api"),
|
2023-09-15 10:55:29 +00:00
|
|
|
}),
|
2023-09-22 16:15:01 +00:00
|
|
|
DB: db,
|
2023-09-27 08:39:44 +00:00
|
|
|
KeyHolder: secrets.NewKeyHolder(secrets.GetOrCreateClISecret()),
|
2023-09-15 10:55:29 +00:00
|
|
|
}
|
|
|
|
err = ctrl.Init()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2023-09-13 08:51:05 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2024-03-08 13:18:38 +00:00
|
|
|
|
|
|
|
if len(os.Args) == 2 && os.Args[1] == "docs" {
|
|
|
|
log.Println("Generating docs")
|
|
|
|
err = cmd.GenerateDocs()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-03-13 06:08:30 +00:00
|
|
|
cmd.Execute(&ctrl, AppVersion)
|
2023-09-12 10:15:41 +00:00
|
|
|
}
|
2023-10-25 02:58:54 +00:00
|
|
|
|
2024-03-08 11:49:59 +00:00
|
|
|
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 {
|
|
|
|
} else {
|
|
|
|
// Config file was found but another error was produced
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 02:58:54 +00:00
|
|
|
// 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") != "" {
|
|
|
|
return os.Getenv("ENTE_CLI_CONFIG_PATH"), nil
|
|
|
|
}
|
|
|
|
// Get the user's home directory
|
|
|
|
homeDir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-10-25 11:52:22 +00:00
|
|
|
cliDBPath := filepath.Join(homeDir, ".ente")
|
2023-10-25 02:58:54 +00:00
|
|
|
|
|
|
|
// Check if the folder already exists, if not, create it
|
|
|
|
if _, err := os.Stat(cliDBPath); os.IsNotExist(err) {
|
|
|
|
err := os.MkdirAll(cliDBPath, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cliDBPath, nil
|
|
|
|
}
|