From 9cead56a353a5517bb5c17c5593233c9c3dc930e Mon Sep 17 00:00:00 2001 From: Allyedge Date: Mon, 6 Jun 2022 10:43:23 +0200 Subject: [PATCH] feat: add extra command --- README.md | 1 + cmd/cli/extra.go | 15 +++++++++++++++ cmd/cli/root.go | 3 ++- package-lock.json | 32 -------------------------------- package.json | 2 +- pkg/extra/docker.go | 34 ++++++++++++++++++++++++++++++++++ pkg/extra/extra.go | 33 +++++++++++++++++++++++++++++++++ pkg/extra/questions.go | 13 +++++++++++++ pkg/util/util.go | 32 +++++++++++++++++++++++++++++++- templates/extra/Dockerfile.js | 11 +++++++++++ templates/extra/Dockerfile.ts | 13 +++++++++++++ 11 files changed, 154 insertions(+), 35 deletions(-) create mode 100644 cmd/cli/extra.go delete mode 100644 package-lock.json create mode 100644 pkg/extra/docker.go create mode 100644 pkg/extra/extra.go create mode 100644 pkg/extra/questions.go create mode 100644 templates/extra/Dockerfile.js create mode 100644 templates/extra/Dockerfile.ts diff --git a/README.md b/README.md index 9009c83..b5e6647 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Our CLI allows you to setup and manage Discord bot projects without writing a single line of code! 😁 **User Friendly**
+🔥 **Fast**
💦 **Simple**
🌱 **Efficient**
💪 **Powerful**
diff --git a/cmd/cli/extra.go b/cmd/cli/extra.go new file mode 100644 index 0000000..cd4871f --- /dev/null +++ b/cmd/cli/extra.go @@ -0,0 +1,15 @@ +package cli + +import ( + "github.com/sern-handler/cli/pkg/extra" + "github.com/spf13/cobra" +) + +var extraCmd = &cobra.Command{ + Use: "extra", + Short: "Add extra tools to your Sern project.", + Long: `Add extra tools to your Sern project to help you with the setup and development.`, + Run: func(cmd *cobra.Command, args []string) { + extra.Execute() + }, +} diff --git a/cmd/cli/root.go b/cmd/cli/root.go index 5bc0f4b..cc6f92a 100644 --- a/cmd/cli/root.go +++ b/cmd/cli/root.go @@ -12,7 +12,7 @@ var rootCmd = &cobra.Command{ Short: "A powerful CLI tool for Sern.", Version: "0.1.1", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Sern CLI") + cmd.Help() }, } @@ -21,6 +21,7 @@ func Execute() { rootCmd.Flags().BoolP("version", "v", false, "The version of the Sern CLI.") rootCmd.SetVersionTemplate("Sern CLI - Version {{.Version}}\n") rootCmd.AddCommand(initCmd) + rootCmd.AddCommand(extraCmd) if err := rootCmd.Execute(); err != nil { fmt.Println(err) diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index c0ceba3..0000000 --- a/package-lock.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "name": "@sern/cli", - "version": "0.1.2", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "@sern/cli", - "version": "0.1.0", - "hasInstallScript": true, - "license": "MIT", - "dependencies": { - "@gzuidhof/go-npm": "^0.1.13" - } - }, - "node_modules/@gzuidhof/go-npm": { - "version": "0.1.13", - "resolved": "https://registry.npmjs.org/@gzuidhof/go-npm/-/go-npm-0.1.13.tgz", - "integrity": "sha512-45nOc+/Elll2f1xrFQU7rrpcpl1AIPgi3x04sGizbUUdtyreM8v2QFcpr5gACfYxt9P9wXPShIMXjve1C406Rg==", - "bin": { - "go-npm": "bin/index.js" - } - } - }, - "dependencies": { - "@gzuidhof/go-npm": { - "version": "0.1.13", - "resolved": "https://registry.npmjs.org/@gzuidhof/go-npm/-/go-npm-0.1.13.tgz", - "integrity": "sha512-45nOc+/Elll2f1xrFQU7rrpcpl1AIPgi3x04sGizbUUdtyreM8v2QFcpr5gACfYxt9P9wXPShIMXjve1C406Rg==" - } - } -} diff --git a/package.json b/package.json index 6f3916a..4778d31 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sern/cli", - "version": "0.1.2", + "version": "0.1.1", "description": "Our CLI allows you to setup and manage Discord bot projects without writing a single line of code!", "scripts": { "postinstall": "go-npm install", diff --git a/pkg/extra/docker.go b/pkg/extra/docker.go new file mode 100644 index 0000000..d159062 --- /dev/null +++ b/pkg/extra/docker.go @@ -0,0 +1,34 @@ +package extra + +import ( + "os" + + "github.com/gookit/color" + "github.com/sern-handler/cli/pkg/util" +) + +func addDockerfile() error { + var dockerfile string + + _, err := os.Stat("tsconfig.json") + + if err != nil { + dockerfile = "Dockerfile.js" + } else { + dockerfile = "Dockerfile.ts" + } + + color.Info.Prompt("Adding Dockerfile...") + + err = util.CopyFile("templates/extra/"+dockerfile, "Dockerfile") + + if err != nil { + color.Error.Prompt("Couldn't add the Dockerfile, exiting.") + + return err + } + + color.Info.Prompt("Successfully added the Dockerfile.") + + return nil +} diff --git a/pkg/extra/extra.go b/pkg/extra/extra.go new file mode 100644 index 0000000..bfa6094 --- /dev/null +++ b/pkg/extra/extra.go @@ -0,0 +1,33 @@ +package extra + +import ( + "os" + + "github.com/AlecAivazis/survey/v2" + "github.com/gookit/color" +) + +func Execute() { + answers := struct { + Extra string + }{} + + err := survey.Ask(questions, &answers) + + if err != nil { + color.Error.Prompt("Adding extras failed, exiting.") + + os.Exit(1) + } + + switch answers.Extra { + case "Dockerfile": + err = addDockerfile() + + if err != nil { + color.Error.Prompt("Adding the Dockerfile failed, exiting.") + + os.Exit(1) + } + } +} diff --git a/pkg/extra/questions.go b/pkg/extra/questions.go new file mode 100644 index 0000000..41f5bad --- /dev/null +++ b/pkg/extra/questions.go @@ -0,0 +1,13 @@ +package extra + +import "github.com/AlecAivazis/survey/v2" + +var questions = []*survey.Question{ + { + Name: "extra", + Prompt: &survey.Select{ + Message: "What extra do you want to add?", + Options: []string{"Dockerfile"}, + }, + }, +} diff --git a/pkg/util/util.go b/pkg/util/util.go index f66b4d0..531304b 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -1,6 +1,10 @@ package util -import "os/exec" +import ( + "io" + "os" + "os/exec" +) type PackageManagers struct { NPM bool @@ -27,3 +31,29 @@ func CheckPackageManagers() PackageManagers { return packageManagers } + +func CopyFile(src, dst string) error { + in, err := os.Open(src) + + if err != nil { + return err + } + + defer in.Close() + + out, err := os.Create(dst) + + if err != nil { + return err + } + + defer out.Close() + + _, err = io.Copy(out, in) + + if err != nil { + return err + } + + return out.Close() +} diff --git a/templates/extra/Dockerfile.js b/templates/extra/Dockerfile.js new file mode 100644 index 0000000..e89be66 --- /dev/null +++ b/templates/extra/Dockerfile.js @@ -0,0 +1,11 @@ +FROM node:latest + +WORKDIR /app + +COPY package.json ./ + +RUN npm install + +COPY . . + +RUN node src/index.js diff --git a/templates/extra/Dockerfile.ts b/templates/extra/Dockerfile.ts new file mode 100644 index 0000000..c6c75fa --- /dev/null +++ b/templates/extra/Dockerfile.ts @@ -0,0 +1,13 @@ +FROM node:latest + +WORKDIR /app + +COPY package.json ./ + +RUN npm install + +COPY . . + +RUN tsc --build + +RUN node dist/index.js