66 "path/filepath"
77 "strings"
88 "time"
9+ "os"
910
1011 "github.com/transifex/cli/pkg/txapi"
1112
@@ -18,25 +19,27 @@ MigrateLegacyConfigFile
1819Edits legacy config files so they contain all the necessary information
1920to use the 3rd version of the API.
2021Steps 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*/
3134func 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
135181func getOrganizationSlug (
0 commit comments