2
\$\begingroup\$

I have written a simple app that adds gitignore files to the current working directory - it does this by reading files hosted on GitHub and then saving them (currently Go, Python and Visual Studio gitignores).

I am a relatively new programmer and mainly code in C# and have written this app in Go. I would be very grateful for any feedback on how I can make any improvements to this code.

Usage:

A user can execute the following to save a Go gitignore to the current working directory (provided the binary is in the user's path):

$ ggi go

App structure:

app_root/
    main.go
    models/
        constants.go

main.go

package main

import (
    "bufio"
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
    "strings"

    "GGI/models"
)

var template string
var url string

func main() {
args := os.Args[1:]
if len(args) != 1 {
    showGgiInfo()
}
if len(args) == 1 {
    template = args[0]
    if gitIgnoreExists() {
        reader := bufio.NewReader(os.Stdin)
        fmt.Print("gitignore already exists, overwrite? (Y / N): ")
        text, _ := reader.ReadString('\n')
        text = strings.ToLower(strings.Trim(text, " \r\n"))

        if strings.Compare(text, "y") == 0 {
            downloadAndSave(template)
        } else {
            os.Exit(1)
        }
    } else {
        downloadAndSave(template)
    }
}
}

func showGgiInfo() {
    fmt.Println("GGI is a tool for installing gitignore templates.")
    fmt.Println("")
    fmt.Println("Usage:")
    fmt.Println("   ggi [parameter]")
    fmt.Println("")
    fmt.Println("Gitignore templates available:")
    fmt.Println("")
    fmt.Println("   Go      go")
    fmt.Println("   Python      py")
    fmt.Println("   Visual Studio   vs")
    fmt.Println("")
    fmt.Println("Example:")
    fmt.Println("   ggi vs (Visual Studio)")
    os.Exit(0)
}

func gitIgnoreExists() bool {
    pwd, err := os.Getwd()

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    f := pwd + "\\.gitignore"

    if _, err := os.Stat(f); err == nil {
        return true
    }
    return false
}

func downloadAndSave(template string) {
    setURL(template)
    resp, err := http.Get(url)
    body, err := ioutil.ReadAll(resp.Body)

    if err != nil {
        fmt.Println("Error")
    } else {

        var write []byte
        write = append(write, body...)
        err = ioutil.WriteFile(".gitignore", write, 0644)
        if err != nil {
            fmt.Print("Error")
        } else {
            fmt.Println(".gitignore added!")
        }
    }
}

func setURL(template string) {
    template = strings.ToLower(template)
    switch template {
    case "go":
        url = constants.Go
    case "py":
        url = constants.Python
    case "vs":
        url = constants.VisualStudio
    default:
        showGgiInfo()
        os.Exit(0)
    }
}

constants.go

package constants

const (
    Go           = "https://raw.githubusercontent.com/github/gitignore/master/Go.gitignore"
    Python       = "https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore"
    VisualStudio = "https://raw.githubusercontent.com/github/gitignore/master/VisualStudio.gitignore"
)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.