|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + // "io/ioutil" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + // "reflect" |
| 10 | + |
| 11 | + "github.com/FourtekIT/devnagri-cli/config" |
| 12 | + "github.com/Jeffail/gabs" |
| 13 | + "github.com/spf13/viper" |
| 14 | + "gopkg.in/resty.v1" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + saveResponseAndConvert() |
| 19 | +} |
| 20 | + |
| 21 | +func decodeBase64(cypher string) string { |
| 22 | + |
| 23 | + data, _ := base64.StdEncoding.DecodeString(cypher) |
| 24 | + |
| 25 | + return string(data) |
| 26 | + |
| 27 | +} |
| 28 | + |
| 29 | +func saveResponseAndConvert() { |
| 30 | + |
| 31 | + var ClientID = config.FetchAndValidate("ClientID") // returns string |
| 32 | + |
| 33 | + var ClientSecret = config.FetchAndValidate("ClientSecret") // returns string |
| 34 | + |
| 35 | + var ProjectKey = config.FetchAndValidate("ProjectKey") // returns string |
| 36 | + |
| 37 | + var AccessToken = config.FetchAndValidate("AccessToken") // returns string |
| 38 | + |
| 39 | + resp, err := resty.R(). |
| 40 | + SetHeader("Accept", "application/json"). |
| 41 | + SetHeader("Content-Type", "multipart/form-data"). |
| 42 | + SetAuthToken(AccessToken). |
| 43 | + SetFormData(map[string]string{ |
| 44 | + "client_id": ClientID, |
| 45 | + "client_secret": ClientSecret, |
| 46 | + "project_key": ProjectKey}). |
| 47 | + Post("http://dev.devnagri.co.in/api/project/pull") |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + panic(err) |
| 51 | + } |
| 52 | + |
| 53 | + //fmt.Println(resp) |
| 54 | + resJson, _ := gabs.ParseJSON([]byte(resp.String())) |
| 55 | + |
| 56 | + fmt.Println(resJson) |
| 57 | + children, _ := resJson.S("file_content").Children() |
| 58 | + child := children[0] |
| 59 | + //fmt.Println(child.String()) |
| 60 | + |
| 61 | + //TODO: Iterate this over all the file names recieved from the remote |
| 62 | + file, err := os.Create("temp.txt") |
| 63 | + if err != nil { |
| 64 | + log.Fatal("Cannot create file", err) |
| 65 | + } |
| 66 | + defer file.Close() |
| 67 | + |
| 68 | + _, err = file.WriteString(child.String()) |
| 69 | + |
| 70 | + // This works perfectly |
| 71 | + //fmt.Println("Decoding the string manually") |
| 72 | + //x := "PCEtLSBUcmFuc2xhdGVkIEJ5IERldm5hZ3JpIC0tPgo8IS0tIGh0dHA6Ly9kZXZuYWdyaS5jb20gLS0+CjxyZXNvdXJjZXMgdG9vbHM6aWdub3JlPSJFeHRyYVRyYW5zbGF0aW9uIiB4bWxuczp0b29scz0iaHR0cDovL3NjaGVtYXMuYW5kcm9pZC5jb20vdG9vbHMiPgogICAgPHN0cmluZyBuYW1lPSJhcHBfbmFtZSI+PC9zdHJpbmc+CiAgICA8c3RyaW5nIG5hbWU9ImhpbnRfYWN0dWFsIj48L3N0cmluZz4KIDwvcmVzb3VyY2VzPg==" |
| 73 | + //decodeBase64(x) |
| 74 | + |
| 75 | + //dat, _ := ioutil.ReadFile("temp.txt") |
| 76 | + //datString := string(dat) |
| 77 | + //fmt.Println("String Length : ", len(datString)) |
| 78 | + |
| 79 | + //content := datString[1:(len(datString) - 1)] |
| 80 | + //fmt.Println(content) |
| 81 | + |
| 82 | + //fileContent := decodeBase64(content) |
| 83 | + //fmt.Println(fileContent) |
| 84 | + |
| 85 | + //fmt.Println("<<< Reading temp file now >>>") |
| 86 | + //fileContent := decodeBase64(string(dat)) |
| 87 | + //fmt.Println(fileContent) |
| 88 | + |
| 89 | + //TODO: This should be abstracted to various languages |
| 90 | + |
| 91 | + //TODO: Read all the languages mentioned in the YAML file |
| 92 | + |
| 93 | + fieldValue := viper.GetStringSlice("TargetLanguages") // returns string |
| 94 | + fmt.Println(fieldValue) |
| 95 | + |
| 96 | + /* |
| 97 | + //TODO: Make the << hi >> dir |
| 98 | + if _, err := os.Stat("./values-hi"); os.IsNotExist(err) { |
| 99 | + os.Mkdir("./values-hi", os.ModePerm) |
| 100 | + } |
| 101 | +
|
| 102 | + //TODO: Store the content of temp into the actual file |
| 103 | +
|
| 104 | + responseFile, err := os.Create("./values-hi/strings.xml") |
| 105 | +
|
| 106 | + if err != nil { |
| 107 | + log.Fatal("Cannot create file", err) |
| 108 | + } |
| 109 | + defer responseFile.Close() |
| 110 | +
|
| 111 | + _, err = responseFile.WriteString(fileContent) |
| 112 | +
|
| 113 | + os.Remove("temp.txt") |
| 114 | +
|
| 115 | + */ |
| 116 | + fmt.Println("Done!") |
| 117 | + |
| 118 | +} |
0 commit comments