diff --git a/.private b/.private new file mode 100644 index 0000000..1b02325 --- /dev/null +++ b/.private @@ -0,0 +1 @@ +bea494ba492727728f9f2518e45d0e43a62d2237 diff --git a/cmd/init.go b/cmd/init.go index 73df35e..2345020 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -45,9 +45,9 @@ var initCmd = &cobra.Command{ Short: "This command creates a .devnagri.yaml file in the repo.", Long: `A longer description of init command`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("init called") - + fmt.Println("Created the default .devnagri.yaml config in the current directory.") createConfigFile() + fmt.Println("Done!") }, } @@ -88,4 +88,5 @@ func createConfigFile() { defer file.Close() fmt.Fprintf(file, string(d)) + } diff --git a/cmd/pull.go b/cmd/pull.go index 305104b..fe5e80c 100644 --- a/cmd/pull.go +++ b/cmd/pull.go @@ -34,7 +34,7 @@ var pullCmd = &cobra.Command{ Short: "This command pulls the translated files from Devnagri", Long: `When all the files for a language have been translated, they can be pulled from the Devnagri platform to the local filesystem using the CLI tool.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("pull called") + fmt.Println("Pulling the files from Devnagri") saveResponseAndConvert() }, } @@ -61,8 +61,12 @@ func saveResponseAndConvert() { var ProjectKey = config.FetchAndValidate("ProjectKey") // returns string + var AccessToken = config.FetchAndValidate("AccessToken") // returns string + resp, err := resty.R(). + SetHeader("Accept", "application/json"). SetHeader("Content-Type", "multipart/form-data"). + SetAuthToken(AccessToken). SetFormData(map[string]string{ "client_id": ClientID, "client_secret": ClientSecret, @@ -73,14 +77,13 @@ func saveResponseAndConvert() { panic(err) } - fmt.Println(resp) - fmt.Println("\n\n") + //fmt.Println(resp) resJson, _ := gabs.ParseJSON([]byte(resp.String())) children, _ := resJson.S("file_content").Children() child := children[0] - fmt.Println(child.String()) + //fmt.Println(child.String()) //TODO: Iterate this over all the file names recieved from the remote file, err := os.Create("temp.txt") @@ -97,13 +100,29 @@ func saveResponseAndConvert() { //decodeBase64(x) dat, _ := ioutil.ReadFile("temp.txt") - //fmt.Println("Reading content.txt") - fileContent := decodeBase64(string(dat)) - fmt.Println(fileContent) + datString := string(dat) + //fmt.Println("String Length : ", len(datString)) + content := datString[1:(len(datString) - 1)] + //fmt.Println(content) + fileContent := decodeBase64(content) + //fmt.Println(fileContent) + //fmt.Println("<<< Reading temp file now >>>") + //fileContent := decodeBase64(string(dat)) + //fmt.Println(fileContent) //TODO: Store the content of temp into the actual file + responseFile, err := os.Create("responseFile.txt") + + if err != nil { + log.Fatal("Cannot create file", err) + } + defer responseFile.Close() + + _, err = responseFile.WriteString(fileContent) //TODO: Delete the temp file + + fmt.Println("Done!") } func decodeBase64(cypher string) string { diff --git a/cmd/push.go b/cmd/push.go index c47b813..7147cc3 100644 --- a/cmd/push.go +++ b/cmd/push.go @@ -15,18 +15,26 @@ package cmd import ( + "crypto/sha256" + "encoding/hex" "fmt" - + "io" + "log" + "os" + //"github.com/FourtekIT/devnagri-cli/utils" + "github.com/FourtekIT/devnagri-cli/config" "github.com/spf13/cobra" + "gopkg.in/resty.v1" ) // pushCmd represents the push command var pushCmd = &cobra.Command{ Use: "push", - Short: "This command pushes the untranslated files from Devnagri", + Short: "This command pushes the untranslated files to Devnagri", Long: `This command transfers all the untranslated local files to the Devnagri platform on a language basis.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("push called") + fmt.Println("Pushing the files from Devnagri") + listAllFilesAndPush() }, } @@ -43,3 +51,57 @@ func init() { // is called directly, e.g.: // pushCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } + +func listAllFilesAndPush() { + var ClientID = config.FetchAndValidate("ClientID") // returns string + + var ClientSecret = config.FetchAndValidate("ClientSecret") // returns string + + var ProjectKey = config.FetchAndValidate("ProjectKey") // returns string + + var AccessToken = config.FetchAndValidate("AccessToken") // returns string + + filename := "./en/CallingPapaPro2.xml" + + resp, err := resty.R(). + SetHeader("Accept", "application/json"). + SetHeader("Content-Type", "multipart/form-data"). + SetAuthToken(AccessToken). + SetFile("file[0][file]", filename). + SetFormData(map[string]string{ + "client_id": ClientID, + "client_secret": ClientSecret, + "project_key": ProjectKey, + "file[0][hash]": sha256Hash(filename), + "file[0][extension]": "xml", + "file[0][file_type]": "xml", + "file[0][location]": filename, + }). + Post("http://dev.devnagri.co.in/api/project/push") + + if err != nil { + panic(err) + } + + fmt.Println(resp) +} + +func sha256Hash(fileName string) string { + + f, err := os.Open(fileName) + if err != nil { + log.Fatal(err) + } + defer f.Close() + + h := sha256.New() + if _, err := io.Copy(h, f); err != nil { + log.Fatal(err) + } + + //fmt.Printf("%x", h.Sum(nil)) + //return h.Sum(nil) + + hashString := hex.EncodeToString(h.Sum(nil)) + return hashString +} diff --git a/cmd/root.go b/cmd/root.go index 4300d28..f0f0970 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -31,7 +31,7 @@ var rootCmd = &cobra.Command{ Short: "The CLI client for the Devnagri platform API", Long: ` devnagri is the cross platform API client for Devnagri written in Go-lang. - + This CLI client has been developed to facilitate the integration of Devnagri platform with a Developer's workflow `, diff --git a/cmd/status.go b/cmd/status.go index eab422c..8fb1112 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -28,10 +28,11 @@ var statusCmd = &cobra.Command{ Short: "This command fetches the status of the current project.", Long: `A long description of status command.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("status called") + fmt.Println("The current status of your projects from Devnagri") fetchStatus() + fmt.Println("Done!") }, } diff --git a/cmd/validate.go b/cmd/validate.go index 177216b..586ff4c 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -30,8 +30,9 @@ var validateCmd = &cobra.Command{ Short: "This command validates the credentials in the .devnagri file within the local folder.", Long: `The validate command is responsible for generating and storing the Access Token which will further enable the usage of pull and push.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("validate called") + fmt.Println("Validating the configuration in .devnagri.yaml and generating the access token.") validate() + fmt.Println("Done!") }, }