Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/txlib/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Config struct {
/*
Load Transifex configuration from the usual paths:

- ~/.transifexrc for the root configuration
- $XDG_CONFIG_HOME/transifex/transifexrc (or ~/.config/transifex/transifexrc if XDG_CONFIG_HOME is not set) for the root configuration, with fallback to ~/.transifexrc for legacy support

- ./.tx/config for the local configuration

Expand Down
23 changes: 22 additions & 1 deletion internal/txlib/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func (rootCfg *RootConfig) save() error {
}

func (rootCfg *RootConfig) saveToPath() error {
dir := filepath.Dir(rootCfg.Path)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}

file, err := os.OpenFile(rootCfg.Path,
os.O_RDWR|os.O_CREATE|os.O_TRUNC,
0755)
Expand Down Expand Up @@ -204,5 +209,21 @@ func GetRootPath() (string, error) {
}
homeDir = usr.HomeDir
}
return filepath.Join(homeDir, ".transifexrc"), nil

xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
if xdgConfigHome == "" {
xdgConfigHome = filepath.Join(homeDir, ".config")
}

xdgPath := filepath.Join(xdgConfigHome, "transifex", "transifexrc")
if _, err := os.Stat(xdgPath); err == nil {
return xdgPath, nil
}

legacyPath := filepath.Join(homeDir, ".transifexrc")
if _, err := os.Stat(legacyPath); err == nil {
return legacyPath, nil
}

return xdgPath, nil
}
25 changes: 14 additions & 11 deletions internal/txlib/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,27 @@ func GetHostAndToken(
// for the hostname
token = selectedHost.Token
} else {
fmt.Println("API token not found. Please provide it and it will " +
"be saved in '~/.transifexrc'.")
fmt.Println("If you don't have an API token, you can generate " +
"one in https://app.transifex.com/user/settings/api/")
fmt.Print("> ")
_, err := fmt.Scanln(&token)
if err != nil {
return "", "", err
}

rootConfigPath := ""
if cfg.Root == nil {
rootConfigPath, err := config.GetRootPath()
var err error
rootConfigPath, err = config.GetRootPath()
if err != nil {
return "", "", err
}
cfg.Root = &config.RootConfig{
Path: rootConfigPath,
}
} else {
rootConfigPath = cfg.Root.Path
}

fmt.Printf("API token not found. Please provide it and it will be saved in '%s'.\n", rootConfigPath)
fmt.Println("If you don't have an API token, you can generate " +
"one in https://app.transifex.com/user/settings/api/")
fmt.Print("> ")
_, err := fmt.Scanln(&token)
if err != nil {
return "", "", err
}
cfg.Root.Hosts = append(cfg.Root.Hosts, config.Host{
Name: hostname,
Expand Down
3 changes: 1 addition & 2 deletions internal/txlib/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ func MigrateLegacyConfigFile(
} else {
// No token for some reason get a new one
if cfg.GetActiveHost() != nil {
fmt.Println("API token not found. Please provide it and it will " +
"be saved in '~/.transifexrc'.")
fmt.Printf("API token not found. Please provide it and it will be saved in '%s'.\n", cfg.Root.Path)
} else {
fmt.Println("Please provide an API token to continue.")
}
Expand Down