-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var createCmd = &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a custom template", | ||
Long: `This command help you generate a custom template for future. | ||
It will release in next version.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("This feature will release in next version.") | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(createCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "Show available templates.", | ||
Long: `This command can show available templates.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
template, _ := cmd.Flags().GetString("template") | ||
list(template) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(listCmd) | ||
listCmd.Flags().StringP("template", "t", "all", "Show default/custom/all templates.") | ||
} | ||
|
||
func list(template string) { | ||
defaultInfo := color.New(color.FgHiBlue, color.Bold).SprintFunc() | ||
customInfo := color.New(color.FgHiRed, color.Bold).SprintFunc() | ||
switch template { | ||
case "all": | ||
fmt.Printf("🍻 %s\n", defaultInfo("Default Template")) | ||
getToptalList() | ||
fmt.Print("\n\n") | ||
fmt.Printf("🥂 %s\n", customInfo("Custom Template")) | ||
getCustomList() | ||
case "default": | ||
fmt.Printf("🍻 %s\n", defaultInfo("Default Template")) | ||
getToptalList() | ||
case "custom": | ||
fmt.Printf("🥂 %s\n", customInfo("Custom Template")) | ||
getCustomList() | ||
default: | ||
fmt.Println("Wrong") | ||
} | ||
} | ||
|
||
func getToptalList() { | ||
API := "https://www.toptal.com/developers/gitignore/api/list" | ||
resp, http_err := http.Get(API) | ||
if http_err != nil { | ||
fmt.Println("Error: An error occurs in the http connection.") | ||
fmt.Println(http_err) | ||
os.Exit(1) | ||
} | ||
|
||
defer resp.Body.Close() | ||
body, read_err := ioutil.ReadAll(resp.Body) | ||
if read_err != nil { | ||
fmt.Println("Error: An error occurs in I/O.") | ||
fmt.Println(read_err) | ||
os.Exit(1) | ||
} | ||
|
||
types := strings.Split(strings.Join(strings.Split(string(body), "\n"), ","), ",") | ||
for _, t := range types { | ||
fmt.Println("➢", t) | ||
} | ||
} | ||
|
||
func getCustomList() { | ||
fmt.Println("No custom templates.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/fatih/color" | ||
homedir "github.com/mitchellh/go-homedir" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var cfgFile string | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "gg", | ||
Short: "Generates gitignore easily🌈", | ||
Long: `GG is a CLI tool to help developers generate | ||
the gitignore which is collected in 'toptal.com' | ||
or create a custom template.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
info := color.New(color.FgHiBlue).SprintFunc() | ||
option := color.New(color.Bold).SprintFunc() | ||
templates, _ := cmd.Flags().GetStringSlice("templates") | ||
file, _ := cmd.Flags().GetString("file") | ||
append, _ := cmd.Flags().GetBool("append") | ||
fmt.Printf("%s %s\n", info("Selected Template:"), option(strings.Join(templates, ", "))) | ||
fmt.Printf("%s %s\n", info("Selected File :"), option(file)) | ||
fmt.Printf("%s %s\n", info("Appending Mode :"), option(strconv.FormatBool(append))) | ||
fmt.Println() | ||
process(templates, file, append) | ||
}, | ||
} | ||
|
||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gg.yaml)") | ||
|
||
rootCmd.Flags().StringSliceP("templates", "t", nil, "template name") | ||
rootCmd.Flags().StringP("file", "f", ".gitignore", "specify filename") | ||
rootCmd.Flags().BoolP("append", "a", false, "toggle append mode") | ||
} | ||
|
||
func initConfig() { | ||
versionInfo := color.New(color.FgHiYellow, color.Bold).SprintFunc() | ||
fmt.Printf("%s ⛄️\n\n", versionInfo("Gitignore Generator v0.0.1")) | ||
|
||
if cfgFile != "" { | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
home, err := homedir.Dir() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
viper.AddConfigPath(home) | ||
viper.SetConfigName(".gg") | ||
} | ||
|
||
viper.AutomaticEnv() | ||
|
||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Println("Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} | ||
|
||
func process(templates []string, file string, append bool) { | ||
content := "" | ||
fmt.Println("🍭 Generating .gitignore") | ||
fmt.Println() | ||
for _, t := range templates { | ||
fmt.Printf("Adding %s.gitignore ...\n", t) | ||
content += fetchGitIgnore(t) | ||
} | ||
saveFile(content, file, append) | ||
fmt.Println() | ||
fmt.Println("🍻 Done.") | ||
} | ||
|
||
func fetchGitIgnore(templateName string) string { | ||
API := fmt.Sprintf("https://www.toptal.com/developers/gitignore/api/%s", templateName) | ||
template := fmt.Sprintf("#### %s ####\n", templateName) | ||
|
||
resp, http_err := http.Get(API) | ||
if http_err != nil { | ||
fmt.Println("Error: An error occurs in the http connection.") | ||
fmt.Println(http_err) | ||
os.Exit(1) | ||
} | ||
defer resp.Body.Close() | ||
body, read_err := ioutil.ReadAll(resp.Body) | ||
if read_err != nil { | ||
fmt.Println("Error: An error occurs in I/O.") | ||
fmt.Println(read_err) | ||
os.Exit(1) | ||
} | ||
template += string(body) | ||
template += "\n" | ||
return template | ||
} | ||
|
||
func saveFile(content string, filename string, append bool) { | ||
var file *os.File | ||
var err error | ||
if append { | ||
file, err = os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
} else { | ||
file, err = os.Create(filename) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
defer func() { | ||
if err := file.Close(); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
if _, err := file.WriteString(content); err != nil { | ||
panic(err) | ||
} | ||
file.Sync() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "gg/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |