Skip to content

Commit 482ef99

Browse files
committed
Migrate www.transifex.com to app.transifex.com in local and root config
1 parent 4e20586 commit 482ef99

File tree

3 files changed

+152
-43
lines changed

3 files changed

+152
-43
lines changed

cmd/tx/main.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,34 @@ func Main() {
7373
Client: client,
7474
}
7575

76-
backUpFilePath, err := txlib.MigrateLegacyConfigFile(&cfg,
76+
backUpFilePath, backUpRootFilePath, err := txlib.MigrateLegacyConfigFile(&cfg,
7777
api)
7878

7979
if err != nil {
8080
return cli.Exit(err, 1)
8181
}
8282
fmt.Printf(
83-
"Migration ended! We have also created a backup "+
84-
"file for your previous config file `%s`.\n",
83+
"Migration ended! We have created a backup "+
84+
"file for your previous "+
85+
cfg.Local.Path+
86+
" at `%s`.\n",
8587
backUpFilePath,
8688
)
89+
if backUpRootFilePath != "" {
90+
fmt.Printf(
91+
"Additionally, we have created a backup "+
92+
"file for your previous "+
93+
cfg.Root.Path+
94+
" at `%s`.\n",
95+
backUpRootFilePath,
96+
)
97+
} else {
98+
fmt.Printf(
99+
"Additionally, we have created a new "+
100+
cfg.Root.Path+
101+
" with your token.\n",
102+
)
103+
}
87104
return nil
88105
},
89106
},

internal/txlib/migrate.go

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"strings"
88
"time"
9+
"os"
910

1011
"github.com/transifex/cli/pkg/txapi"
1112

@@ -18,25 +19,27 @@ MigrateLegacyConfigFile
1819
Edits legacy config files so they contain all the necessary information
1920
to use the 3rd version of the API.
2021
Steps taken:
21-
1. Check for token setting.
22+
1. Update 'host' field in 'main' section of local configuration to use app.transifex.com
23+
2. If root configuration has www.transifex.com, update it to app.transifex.com instead
24+
3. Check for token setting.
2225
If not found check for API token in the old configuration.
2326
If not found generate one.
24-
2. Check for rest_hostname setting. If not found add it.
25-
3. Check the section keys are using the legacy format
27+
4. Check for rest_hostname setting. If not found add it.
28+
5. Check the section keys are using the legacy format
2629
(`<project_slug>.<resource_slug>`)
2730
If yes find the organization for each section key and reformat the
2831
section key to conform to the new format
2932
(o:<organization_slug>:p:<project_slug>:r:<resource_slug>)
3033
*/
3134
func MigrateLegacyConfigFile(
3235
cfg *config.Config, api jsonapi.Connection,
33-
) (string, error) {
36+
) (string, string, error) {
3437
// Backup previous file before doing anything
3538

3639
//Read all the contents of the original config file
3740
bytesRead, err := ioutil.ReadFile(cfg.Local.Path)
3841
if err != nil {
39-
return "", fmt.Errorf("aborting, could not create backup file %w", err)
42+
return "", "", fmt.Errorf("aborting, could not read local configuration %w", err)
4043
}
4144

4245
//Copy all the contents to the destination file
@@ -48,14 +51,52 @@ func MigrateLegacyConfigFile(
4851
err = ioutil.WriteFile(backUpFilePath, bytesRead, 0755)
4952

5053
if err != nil {
51-
return "", fmt.Errorf("aborting, could not create backup file %w", err)
54+
return "", "", fmt.Errorf("aborting, could not create backup file %w", err)
55+
}
56+
57+
// Also backup the root configuration file, if it exists
58+
backUpRootFilePath := ""
59+
rootFileCreated := false
60+
if _, err = os.Stat(cfg.Root.Path); err == nil {
61+
bytesRead, err = ioutil.ReadFile(cfg.Root.Path)
62+
if err != nil {
63+
return "", "", fmt.Errorf("aborting, could not read root configuration %w", err)
64+
}
65+
backUpRootFilePath = filepath.Join(filepath.Dir(cfg.Root.Path),
66+
".transifexrc_"+currentTime.Format("20060102150405")+".bak")
67+
err = ioutil.WriteFile(backUpRootFilePath, bytesRead, 0755)
68+
if err != nil {
69+
return "", "", fmt.Errorf("aborting, could not create backup file %w", err)
70+
}
71+
} else if os.IsNotExist(err) {
72+
fmt.Printf("Root configuration file not found -- creating it at `%s`.\n", cfg.Root.Path)
73+
f, err := os.Create(cfg.Root.Path)
74+
if err != nil {
75+
return "", "", fmt.Errorf("aborting, could not create root configuration %w", err)
76+
}
77+
rootFileCreated = true
78+
defer f.Close()
79+
} else {
80+
return "", "", fmt.Errorf("aborting, could not read root configuration %w", err)
81+
}
82+
83+
// Update 'host' field in 'main' section of local config to use app.transifex.com
84+
cfg.Local.Host = strings.ReplaceAll(cfg.Local.Host, "www.transifex.com", "app.transifex.com")
85+
86+
// Update existing root config to use app.transifex.com
87+
for i := range cfg.Root.Hosts {
88+
host := &cfg.Root.Hosts[i]
89+
host.Name = strings.ReplaceAll(host.Name, "www.transifex.com", "app.transifex.com")
5290
}
5391

5492
// Get the current host
5593
activeHost := cfg.GetActiveHost()
5694

5795
if activeHost == nil {
5896
activeHost = &config.Host{}
97+
activeHost.Name = "https://app.transifex.com"
98+
activeHost.RestHostname = ""
99+
activeHost.Token = ""
59100
}
60101

61102
if activeHost.Token == "" {
@@ -78,18 +119,23 @@ func MigrateLegacyConfigFile(
78119
var token string
79120
_, err := fmt.Scanln(&token)
80121
if err != nil {
81-
return "", err
122+
return "", "", err
82123
}
83124
activeHost.Token = token
84125
}
85126
}
86127

87128
// Save the new rest url
88129
if activeHost.RestHostname == "" {
89-
fmt.Printf("No rest_hostname found adding `rest.api.transifex.com `\n")
130+
fmt.Println("No rest_hostname found. Adding `rest.api.transifex.com`")
90131
activeHost.RestHostname = "https://rest.api.transifex.com"
91132
}
92133

134+
// Save the new root config if we created the file
135+
if rootFileCreated {
136+
cfg.Root.Hosts = append(cfg.Root.Hosts, *activeHost)
137+
}
138+
93139
// Try to update resources currently in config
94140
// Internally if config finds a resource without ":" it will treat it as
95141
// a migration, read the resource in a special way and create a temp
@@ -102,7 +148,7 @@ func MigrateLegacyConfigFile(
102148
if resource.OrganizationSlug == "" {
103149
organizationSlug, err := getOrganizationSlug(api, &resource)
104150
if err != nil {
105-
return "", err
151+
return "", "", err
106152
}
107153
if organizationSlug == "" {
108154
fmt.Printf(
@@ -127,9 +173,9 @@ func MigrateLegacyConfigFile(
127173
cfg.Local.Resources = resources
128174
err = cfg.Save()
129175
if err != nil {
130-
return "", fmt.Errorf("%w", err)
176+
return "", "", fmt.Errorf("%w", err)
131177
}
132-
return backUpFilePath, nil
178+
return backUpFilePath, backUpRootFilePath, nil
133179
}
134180

135181
func getOrganizationSlug(

0 commit comments

Comments
 (0)