Skip to content

Migrate www.transifex.com to app.transifex.com in local and root config #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
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
20 changes: 17 additions & 3 deletions cmd/tx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,31 @@ func Main() {
Client: client,
}

backUpFilePath, err := txlib.MigrateLegacyConfigFile(&cfg,
backUpFilePath, backUpRootFilePath, err := txlib.MigrateLegacyConfigFile(&cfg,
api)

if err != nil {
return cli.Exit(err, 1)
}
fmt.Printf(
"Migration ended! We have also created a backup "+
"file for your previous config file `%s`.\n",
"Migration ended! We have created a backup "+
"file for your previous `%s` at `%s`.\n",
cfg.Local.Path,
backUpFilePath,
)
if backUpRootFilePath != "" {
fmt.Printf(
"Additionally, we have created a backup "+
"file for your previous `%s` at `%s`.\n",
cfg.Root.Path,
backUpRootFilePath,
)
} else {
fmt.Printf(
"Additionally, we have created a new `%s` with your token.\n",
cfg.Root.Path,
)
}
return nil
},
},
Expand Down
68 changes: 57 additions & 11 deletions internal/txlib/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"
"time"
"os"

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

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

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

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

if err != nil {
return "", fmt.Errorf("aborting, could not create backup file %w", err)
return "", "", fmt.Errorf("aborting, could not create backup file %w", err)
}

// Also backup the root configuration file, if it exists
backUpRootFilePath := ""
rootFileCreated := false
if _, err = os.Stat(cfg.Root.Path); err == nil {
bytesRead, err = ioutil.ReadFile(cfg.Root.Path)
if err != nil {
return "", "", fmt.Errorf("aborting, could not read root configuration %w", err)
}
backUpRootFilePath = filepath.Join(filepath.Dir(cfg.Root.Path),
".transifexrc_"+currentTime.Format("20060102150405")+".bak")
err = ioutil.WriteFile(backUpRootFilePath, bytesRead, 0755)
if err != nil {
return "", "", fmt.Errorf("aborting, could not create backup file %w", err)
}
} else if os.IsNotExist(err) {
fmt.Printf("Root configuration file not found -- creating it at `%s`.\n", cfg.Root.Path)
f, err := os.Create(cfg.Root.Path)
if err != nil {
return "", "", fmt.Errorf("aborting, could not create root configuration %w", err)
}
rootFileCreated = true
defer f.Close()
} else {
return "", "", fmt.Errorf("aborting, could not read root configuration %w", err)
}

// Update 'host' field in 'main' section of local config to use app.transifex.com
cfg.Local.Host = strings.ReplaceAll(cfg.Local.Host, "www.transifex.com", "app.transifex.com")

// Update existing root config to use app.transifex.com
for i := range cfg.Root.Hosts {
host := &cfg.Root.Hosts[i]
host.Name = strings.ReplaceAll(host.Name, "www.transifex.com", "app.transifex.com")
}

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

if activeHost == nil {
activeHost = &config.Host{}
activeHost.Name = "https://app.transifex.com"
activeHost.RestHostname = ""
activeHost.Token = ""
}

if activeHost.Token == "" {
Expand All @@ -78,18 +119,23 @@ func MigrateLegacyConfigFile(
var token string
_, err := fmt.Scanln(&token)
if err != nil {
return "", err
return "", "", err
}
activeHost.Token = token
}
}

// Save the new rest url
if activeHost.RestHostname == "" {
fmt.Printf("No rest_hostname found adding `rest.api.transifex.com `\n")
fmt.Println("No rest_hostname found. Adding `rest.api.transifex.com`")
activeHost.RestHostname = "https://rest.api.transifex.com"
}

// Save the new root config if we created the file
if rootFileCreated {
cfg.Root.Hosts = append(cfg.Root.Hosts, *activeHost)
}

// Try to update resources currently in config
// Internally if config finds a resource without ":" it will treat it as
// a migration, read the resource in a special way and create a temp
Expand All @@ -102,7 +148,7 @@ func MigrateLegacyConfigFile(
if resource.OrganizationSlug == "" {
organizationSlug, err := getOrganizationSlug(api, &resource)
if err != nil {
return "", err
return "", "", err
}
if organizationSlug == "" {
fmt.Printf(
Expand All @@ -127,9 +173,9 @@ func MigrateLegacyConfigFile(
cfg.Local.Resources = resources
err = cfg.Save()
if err != nil {
return "", fmt.Errorf("%w", err)
return "", "", fmt.Errorf("%w", err)
}
return backUpFilePath, nil
return backUpFilePath, backUpRootFilePath, nil
}

func getOrganizationSlug(
Expand Down
Loading