diff --git a/.gitignore b/.gitignore index 5009100..67a7008 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ index.js release linux -.DS_Store \ No newline at end of file +.DS_Store +snippets diff --git a/CONTRIB.md b/CONTRIB.md new file mode 100644 index 0000000..20d53e4 --- /dev/null +++ b/CONTRIB.md @@ -0,0 +1,9 @@ +Contributions are welcome and here is what we hope to be the complete information you need. + +# tools + +To ease your work on the project, a set of tasks have been defined using the tool [Task](https://taskfile.dev/). +The installation is as easy and very well described in the [documentation of Task](https://taskfile.dev/installation/). + +If you use VS Code, you can then use the extension [task.vscode-task](https://marketplace.visualstudio.com/items?itemName=task.vscode-task) +For other tools, see the [documentation on integration](https://taskfile.dev/integrations/) \ No newline at end of file diff --git a/Taskfile.yml b/Taskfile.yml new file mode 100644 index 0000000..8b4101a --- /dev/null +++ b/Taskfile.yml @@ -0,0 +1,27 @@ +version: '3' + +tasks: + build-release: + cmds: + - | + ./build-release.sh + + create-release: + cmds: + - ./create-release.sh + + docker-build-image: + cmds: + - ./docker-build-image.sh + + remote-run: + cmds: + - ./remote-run.sh + + remove-release: + cmds: + - ./remove-release.sh + + test: + cmds: + - go test -v ./... \ No newline at end of file diff --git a/help.md b/help.md index e2a0b17..fa33c0d 100644 --- a/help.md +++ b/help.md @@ -1,3 +1,11 @@ # Help on `snippets` This is a work in progress 🚧 + +Available commands are: + +help + +version + +generate \ No newline at end of file diff --git a/internal/internal.go b/internal/internal.go new file mode 100644 index 0000000..793a15f --- /dev/null +++ b/internal/internal.go @@ -0,0 +1,103 @@ +package internal + +import ( + "encoding/json" + "errors" + "os" + "strings" + + _ "embed" + + "github.com/go-resty/resty/v2" + "gopkg.in/yaml.v3" +) + +type YamlSnippet struct { + Name string + Description string + Prefix string + Scope string + Body string +} + +func ReadYamlFile(yamlFilePath string) (map[string]YamlSnippet, error) { + + yamlFile, err := os.ReadFile(yamlFilePath) + + if err != nil { + return nil, err + } + + data := make(map[string]YamlSnippet) + + err = yaml.Unmarshal(yamlFile, &data) + + if err != nil { + return nil, err + } + return data, nil +} + +func GenerateVSCodeSnippets(yamlSnippets map[string]YamlSnippet) ([]byte, error) { + VSCodeSnippets := make(map[string]interface{}) + + for _, snippet := range yamlSnippets { + body := strings.Split(snippet.Body, "\n") + + VSCodeSnippets[snippet.Name] = map[string]interface{}{ + "prefix": snippet.Prefix, + "description": snippet.Description, + "scope": snippet.Scope, + "body": body, + } + + } + + bSnippets, err := json.MarshalIndent(VSCodeSnippets, "", " ") + + if err != nil { + return nil, err + } + return bSnippets, nil +} + +func WriteJsonFile(jsonFilePath string, jsonData []byte) error { + + f, err := os.Create(jsonFilePath) + + if err != nil { + return err + } + + defer f.Close() + + _, err = f.WriteString(string(jsonData)) + + if err != nil { + return err + } + return nil +} + +func DownloadYamlFile(yamlFileUrl, authHeaderName, authHeaderValue, saveFilePath string) error { + // authenticationHeader: + // Example: "PRIVATE-TOKEN: ${GITLAB_WASM_TOKEN}" + client := resty.New() + + if authHeaderName != "" { + client.SetHeader(authHeaderName, authHeaderValue) + } + + resp, err := client.R(). + SetOutput(saveFilePath). + Get(yamlFileUrl) + + if resp.IsError() { + return errors.New("error while downloading the yaml file") + } + + if err != nil { + return err + } + return nil +} diff --git a/snippets_test.go b/internal/internal_test.go similarity index 74% rename from snippets_test.go rename to internal/internal_test.go index 57d8c48..3572a69 100644 --- a/snippets_test.go +++ b/internal/internal_test.go @@ -1,4 +1,4 @@ -package snippets +package internal import ( "os" @@ -6,20 +6,20 @@ import ( ) func TestGenerateVSCodeSnippets(t *testing.T) { - yamlFilePath := "samples/python01.yml" - yamlData, err := readYamlFile(yamlFilePath) + yamlFilePath := "../samples/python01.yml" + yamlData, err := ReadYamlFile(yamlFilePath) if err != nil { t.Errorf("failed to read YAML file: %v", err) return } - jsonSnippets, err := generateVSCodeSnippets(yamlData) + jsonSnippets, err := GenerateVSCodeSnippets(yamlData) if err != nil { t.Errorf("failed to generate JSON snippets: %v", err) return } - codeSnippetsFilePath := "samples/python01.code-snippets" + codeSnippetsFilePath := "../samples/python01.code-snippets" expectedJsonSnippets, err := os.ReadFile(codeSnippetsFilePath) if err != nil { t.Errorf("failed to read code snippets file: %v", err) diff --git a/main.go b/main.go new file mode 100644 index 0000000..eaff096 --- /dev/null +++ b/main.go @@ -0,0 +1,92 @@ +package main + +import ( + _ "embed" + "flag" + "fmt" + "os" + "snippet/internal" +) + +//go:embed version.txt +var version []byte + +//go:embed help.md +var help []byte + +func parse(command string, args []string) error { + switch command { + + case "generate", "gen": + + flagSet := flag.NewFlagSet("generate", flag.ExitOnError) + + input := flagSet.String("input", "", "input yaml file path") + output := flagSet.String("output", "", "output json file path") + + yamFileUrl := flagSet.String("url", "", "Url to download the yaml file") + authHeaderName := flagSet.String("auth-header-name", "", "Authentication header name, ex: PRIVATE-TOKEN") + authHeaderValue := flagSet.String("auth-header-value", "", "Value of the authentication header, ex: IlovePandas") + + flagSet.Parse(args[0:]) + + if *yamFileUrl != "" { // we need to download the yaml file + fmt.Println("🌍 downloading ", *yamFileUrl, "...") + err := internal.DownloadYamlFile(*yamFileUrl, *authHeaderName, *authHeaderValue, *input) + if err != nil { + fmt.Println("😡", err.Error()) + os.Exit(1) + } + } + + yamlData, err := internal.ReadYamlFile(*input) + if err != nil { + fmt.Println("😡", err.Error()) + os.Exit(1) + } + jsonData, err := internal.GenerateVSCodeSnippets(yamlData) + if err != nil { + fmt.Println("😡", err.Error()) + os.Exit(1) + } + + err = internal.WriteJsonFile(*output, jsonData) + + if err != nil { + fmt.Println("😡", err.Error()) + os.Exit(1) + } + fmt.Println("🙂", *output, "generated") + //os.Exit(0) + return nil + + case "version": + fmt.Println(string(version)) + //os.Exit(0) + return nil + + case "help": + fmt.Println(string(help)) + //os.Exit(0) + return nil + + default: + return fmt.Errorf("🔴 invalid command") + } +} + +func main() { + + flag.Parse() + + if len(flag.Args()) < 1 { + fmt.Println(string(help)) + fmt.Println("🔴 invalid command") + os.Exit(0) + } + + command := flag.Args()[0] + + parse(command, flag.Args()[1:]) + +} diff --git a/snippets.go b/snippets.go deleted file mode 100644 index fa6e355..0000000 --- a/snippets.go +++ /dev/null @@ -1,187 +0,0 @@ -package snippets - -import ( - "encoding/json" - "errors" - "flag" - "fmt" - "os" - "strings" - - _ "embed" - - "github.com/go-resty/resty/v2" - "gopkg.in/yaml.v3" -) - -type YamlSnippet struct { - Name string - Description string - Prefix string - Scope string - Body string -} - -//go:embed version.txt -var version []byte - -//go:embed help.md -var help []byte - -func readYamlFile(yamlFilePath string) (map[string]YamlSnippet, error) { - - yamlFile, err := os.ReadFile(yamlFilePath) - - if err != nil { - return nil, err - } - - data := make(map[string]YamlSnippet) - - err = yaml.Unmarshal(yamlFile, &data) - - if err != nil { - return nil, err - } - return data, nil -} - -func generateVSCodeSnippets(yamlSnippets map[string]YamlSnippet) ([]byte, error) { - VSCodeSnippets := make(map[string]interface{}) - - for _, snippet := range yamlSnippets { - body := strings.Split(snippet.Body, "\n") - - VSCodeSnippets[snippet.Name] = map[string]interface{}{ - "prefix": snippet.Prefix, - "description": snippet.Description, - "scope": snippet.Scope, - "body": body, - } - - } - - bSnippets, err := json.MarshalIndent(VSCodeSnippets, "", " ") - - if err != nil { - return nil, err - } - return bSnippets, nil -} - -func writeJsonFile(jsonFilePath string, jsonData []byte) error { - - f, err := os.Create(jsonFilePath) - - if err != nil { - return err - } - - defer f.Close() - - _, err = f.WriteString(string(jsonData)) - - if err != nil { - return err - } - return nil -} - -func downloadYamlFile(yamlFileUrl, authHeaderName, authHeaderValue, saveFilePath string) error { - // authenticationHeader: - // Example: "PRIVATE-TOKEN: ${GITLAB_WASM_TOKEN}" - client := resty.New() - - if authHeaderName != "" { - client.SetHeader(authHeaderName, authHeaderValue) - } - - resp, err := client.R(). - SetOutput(saveFilePath). - Get(yamlFileUrl) - - if resp.IsError() { - return errors.New("error while downloading the yaml file") - } - - if err != nil { - return err - } - return nil -} - -func parse(command string, args []string) error { - switch command { - - case "generate", "gen": - - flagSet := flag.NewFlagSet("generate", flag.ExitOnError) - - input := flagSet.String("input", "", "input yaml file path") - output := flagSet.String("output", "", "output json file path") - - yamFileUrl := flagSet.String("url", "", "Url to download the yaml file") - authHeaderName := flagSet.String("auth-header-name", "", "Authentication header name, ex: PRIVATE-TOKEN") - authHeaderValue := flagSet.String("auth-header-value", "", "Value of the authentication header, ex: IlovePandas") - - flagSet.Parse(args[0:]) - - if *yamFileUrl != "" { // we need to download the yaml file - fmt.Println("🌍 downloading ", *yamFileUrl, "...") - err := downloadYamlFile(*yamFileUrl, *authHeaderName, *authHeaderValue, *input) - if err != nil { - fmt.Println("😡", err.Error()) - os.Exit(1) - } - } - - yamlData, err := readYamlFile(*input) - if err != nil { - fmt.Println("😡", err.Error()) - os.Exit(1) - } - jsonData, err := generateVSCodeSnippets(yamlData) - if err != nil { - fmt.Println("😡", err.Error()) - os.Exit(1) - } - - err = writeJsonFile(*output, jsonData) - - if err != nil { - fmt.Println("😡", err.Error()) - os.Exit(1) - } - fmt.Println("🙂", *output, "generated") - //os.Exit(0) - return nil - - case "version": - fmt.Println(string(version)) - //os.Exit(0) - return nil - - case "help": - fmt.Println(string(help)) - //os.Exit(0) - return nil - - default: - return fmt.Errorf("🔴 invalid command") - } -} - -func main() { - - flag.Parse() - - if len(flag.Args()) < 1 { - fmt.Println("🔴 invalid command") - os.Exit(0) - } - - command := flag.Args()[0] - - parse(command, flag.Args()[1:]) - -}