cmd

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2016 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const REMOVE_GITIGNORE_REPO_URL = "https://github.com/github/gitignore.git"

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "git-ignore",
	Short: "Setup .gitignore file",
	Run: func(cmd *cobra.Command, args []string) {
		if listLangs == true {
			var localGitignoreRepoPath = getGitIgnoreRepoPath()
			cloneGitIgnoreRepoIfNecessary(localGitignoreRepoPath)

			langs := getGitIgnoreLangs(localGitignoreRepoPath)
			for _, lang := range langs {
				fmt.Println(strings.ToLower(lang))
			}
		} else if language != "" {
			var localGitignoreRepoPath = getGitIgnoreRepoPath()
			cloneGitIgnoreRepoIfNecessary(localGitignoreRepoPath)

			langs := getGitIgnoreLangs(localGitignoreRepoPath)
			for _, lang := range langs {
				if strings.ToLower(lang) == strings.ToLower(language) {
					templateFilePath := fmt.Sprintf("%s/%s.gitignore", localGitignoreRepoPath, lang)
					cwd, err := os.Getwd()
					if err != nil {
						os.Stderr.WriteString(fmt.Sprintf("Cannot get CWD: %s", err))
					}

					gitIgnoreFilePath := cwd + "/.gitignore"
					templateFileBody, err := ioutil.ReadFile(templateFilePath)
					if err != nil {
						os.Stderr.WriteString(fmt.Sprintf("Cannot read file %s: %s", templateFilePath, err))
						return
					}

					mode := os.O_WRONLY | os.O_CREATE
					if shouldAppend == true {
						mode = mode | os.O_APPEND
					}
					f, err := os.OpenFile(gitIgnoreFilePath, mode, 0644)
					defer f.Close()
					if err != nil {
						os.Stderr.WriteString(fmt.Sprintf("Cannot open file %s for writing: %s", gitIgnoreFilePath, err))
						return
					}

					if _, err := f.Write(templateFileBody); err != nil {
						os.Stderr.WriteString(fmt.Sprintf("Error while writing %s: %s", gitIgnoreFilePath, err))
						return
					}
					return
				}
			}
			os.Stderr.WriteString(fmt.Sprintf("No template for %s", language))
		} else {
			os.Stderr.WriteString("No action provided.\nPlease see git ignore -h for help\n")
		}
	},
}

Functions

func Execute

func Execute()

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL