Skip to content

Commit

Permalink
Merge branch 'staging'
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi18av committed Jan 17, 2018
2 parents bf1cb44 + 69b5ea6 commit 25612b5
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 15 deletions.
1 change: 1 addition & 0 deletions .private
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bea494ba492727728f9f2518e45d0e43a62d2237
5 changes: 3 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
},
}

Expand Down Expand Up @@ -88,4 +88,5 @@ func createConfigFile() {
defer file.Close()

fmt.Fprintf(file, string(d))

}
33 changes: 26 additions & 7 deletions cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
}
Expand All @@ -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,
Expand All @@ -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")
Expand All @@ -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 {
Expand Down
68 changes: 65 additions & 3 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
}

Expand All @@ -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
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`,
Expand Down
3 changes: 2 additions & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
},
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
},
}

Expand Down

0 comments on commit 25612b5

Please sign in to comment.