├── .gitignore ├── README.md ├── cmd ├── build.go ├── create.go ├── init.go ├── main.go ├── mod.go ├── update.go └── vendor.go ├── go.mod ├── go.sum ├── internal ├── interfaces.go ├── logger.go └── utils.go ├── main.go └── static ├── gpm.gif └── gpm.png /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.exe 3 | *.test 4 | *.prof 5 | 6 | gpm 7 | 8 | .DS_Store 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gpm (Go Project Manager) 2 | 3 | ![Image](/static/gpm.png) 4 | 5 | gpm is a tool for managing Go projects. 6 | 7 | # Overview 8 | 9 | gpm provides commands: 10 | * [To create boilerplate project directory](#create) 11 | * [To build project either using vendor or modules (inside/outside GOPATH)](#build) 12 | * [To add vendor support using modules](#vendor) 13 | * [To add module support (inside/outside GOPATH)](#modules) 14 | 15 | Supporting commands: 16 | * [To update Go version](#update) 17 | 18 | # Preview - Update Go Easily ;) 19 | 20 | ![Gif](/static/gpm.gif) 21 | 22 | # Setup 23 | 24 | ```bash 25 | go get github.com/yashishdua/gpm 26 | ``` 27 | 28 | # Commands 29 | 30 | ## Help 31 | 32 | ```bash 33 | $ gpm 34 | 35 | Usage: 36 | gpm [command] 37 | 38 | Available Commands: 39 | build Build the project 40 | create Creates directory structure 41 | help Help about any command 42 | init Initializes the project 43 | mod Creates modules file 44 | update Updates Go version 45 | vendor Creates vendor using modules 46 | version Print the version number of gpm 47 | 48 | Flags: 49 | -h, --help help for gpm 50 | 51 | Use "gpm [command] --help" for more information about a command. 52 | ``` 53 | 54 | ## Initialize 55 | Make sure to initialize project with gpm to use any command. 56 | 57 | ```bash 58 | $ gpm init 59 | 60 | # Initializing gpm... 61 | gpm: Initialized 62 | ``` 63 | 64 | ## Create 65 | This commands creates a boilerplate project structure. 66 | 67 | ```bash 68 | $ gpm create 69 | 70 | # Setting up project structure... 71 | gpm: Creating cmd directory 72 | gpm: Creating internal directory 73 | gpm: Creating pkg directory 74 | gpm: Creating scripts directory 75 | gpm: Creating api directory 76 | gpm: Creating test directory 77 | gpm: Adding empty main.go 78 | gpm: Create successful 79 | ``` 80 | 81 | ## Build 82 | This command builds project using vendor or modules as specified and also takes care of whether the project is inside or outside GOPATH. 83 | 84 | - To build using modules 85 | ```bash 86 | $ gpm build -m 87 | ``` 88 | 89 | - To build using vendor 90 | ```bash 91 | $ gpm build -v 92 | ``` 93 | 94 | ## Update 95 | This commands updates Go version to specified version. If version not specified, uses default 1.12.5. 96 | 97 | ```bash 98 | $ gpm update -v=1.12.1 99 | 100 | # Updating Go version... 101 | gpm: Uninstalling previous version 102 | gpm: Download go1.12.5.darwin-amd64.tar.gz binary 103 | gpm: Extracting Go archive 104 | gpm: Go updated successfuly 105 | ``` 106 | 107 | ## Vendor 108 | This commands help to create vendor using modules. 109 | 110 | ```bash 111 | $ gpm vendor 112 | 113 | # Creating vendor... 114 | gpm: using modules to build vendor 115 | gpm: Vendor created 116 | ``` 117 | 118 | ## Modules 119 | This commands help to create a module file support. It takes care of whether the project is inside or outside GOPATH. 120 | 121 | ```bash 122 | $ gpm mod 123 | 124 | # Creating modules file... 125 | gpm: Enter module name: 126 | github.com/yashishdua/gpm 127 | go: creating new go.mod: module github.com/yashishdua/gpm 128 | ``` 129 | -------------------------------------------------------------------------------- /cmd/build.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | 7 | "github.com/yashishdua/gpm/internal" 8 | ) 9 | 10 | func Build(flags internal.Flags) { 11 | internal.PrintDescribe("Building...") 12 | 13 | buildScript := getBuildScript(flags.Vendor, flags.Modules) 14 | if buildScript == "" { 15 | internal.PrintError(errors.New("Build failed")) 16 | return 17 | } 18 | 19 | if scriptErr := internal.ConfigureScript(buildScript).Run(); scriptErr != nil { 20 | internal.PrintError(scriptErr) 21 | return 22 | } 23 | 24 | internal.PrintStep("Build successful") 25 | } 26 | 27 | func getBuildScript(vendorFlag bool, modFlag bool) string { 28 | buildScript := `go build` 29 | 30 | if modFlag { 31 | if modExist, _ := internal.CheckFileExist("go.mod"); modExist { 32 | dir, dirErr := internal.GetCurrentDir() 33 | if dirErr != nil { 34 | internal.PrintError(dirErr) 35 | return "" 36 | } 37 | 38 | if insideGoPath := internal.CheckInsideGoPath(dir); insideGoPath { // Inside GOPATH 39 | internal.PrintStep("Using modules to build inside GOPATH") 40 | buildScript = fmt.Sprintf(`GO111MODULE=on %s`, buildScript) 41 | } 42 | 43 | return buildScript 44 | } 45 | 46 | internal.PrintStep("modules doesn't exist") 47 | return "" 48 | } 49 | 50 | if vendorFlag { 51 | if vendorExist, _ := internal.CheckFileExist("vendor"); vendorExist { 52 | dir, dirErr := internal.GetCurrentDir() 53 | if dirErr != nil { 54 | internal.PrintError(dirErr) 55 | return "" 56 | } 57 | 58 | if insideGoPath := internal.CheckInsideGoPath(dir); insideGoPath { // Inside GOPATH 59 | internal.PrintStep("Using vendor to build inside GOPATH") 60 | buildScript = fmt.Sprintf(`GO111MODULE=on %s -mod=vendor`, buildScript) 61 | } 62 | 63 | return buildScript 64 | } 65 | 66 | internal.PrintStep("vendor doesn't exist") 67 | return "" 68 | } 69 | 70 | internal.PrintStep("no vendor or modules were present") 71 | return "" 72 | } 73 | -------------------------------------------------------------------------------- /cmd/create.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io/ioutil" 7 | "os/exec" 8 | "strconv" 9 | 10 | "github.com/yashishdua/gpm/internal" 11 | ) 12 | 13 | // Array cannot me made constant in Go 14 | var dirs = []string{"cmd", "internal", "pkg", "scripts", "api", "test"} 15 | 16 | func SetupProject() { 17 | internal.PrintDescribe("Setting up project structure...") 18 | 19 | for _, dir := range dirs { 20 | internal.PrintStep("Creating " + dir + " directory") 21 | if err := execSetupScript(dir); err != nil { 22 | internal.PrintError(err) 23 | return 24 | } 25 | } 26 | 27 | if goFileExist, _ := internal.CheckFileExist("main.go"); !goFileExist { 28 | internal.PrintStep("Adding empty main.go") 29 | if err := addEmptyGoFile(); err != nil { 30 | internal.PrintError(err) 31 | return 32 | } 33 | } 34 | 35 | internal.PrintStep("Create successful") 36 | } 37 | 38 | func execSetupScript(dir string) error { 39 | scripts := getScripts(dir) 40 | script, countScript, keepScript := scripts[0], scripts[1], scripts[2] 41 | 42 | if _, scriptErr := exec.Command("/bin/sh", "-c", script).Output(); scriptErr != nil { 43 | return scriptErr 44 | } 45 | 46 | cmd := exec.Command("/bin/sh", "-c", countScript) 47 | stdout, err := cmd.StdoutPipe() 48 | if err != nil { 49 | return err 50 | } 51 | 52 | if err := cmd.Start(); err != nil { 53 | return err 54 | } 55 | 56 | scanner := bufio.NewScanner(stdout) 57 | scanner.Split(bufio.ScanWords) 58 | for scanner.Scan() { 59 | value, _ := strconv.Atoi(scanner.Text()) 60 | if value == 0 { 61 | if _, scriptErr := exec.Command("/bin/sh", "-c", keepScript).Output(); scriptErr != nil { 62 | return scriptErr 63 | } 64 | return nil 65 | } 66 | } 67 | 68 | if err := scanner.Err(); err != nil { 69 | return err 70 | } 71 | 72 | return nil 73 | } 74 | 75 | func getScripts(dir string) []string { 76 | script := fmt.Sprintf(`mkdir -p %s`, dir) 77 | countScript := fmt.Sprintf(`cd %s && ls | wc -l`, dir) 78 | keepScript := fmt.Sprintf(`cd %s && touch .keep`, dir) 79 | return []string{script, countScript, keepScript} 80 | } 81 | 82 | func addEmptyGoFile() error { 83 | data := []byte("package main\n\nfunc main() {}") 84 | 85 | if err := ioutil.WriteFile("main.go", data, 0777); err != nil { 86 | return err 87 | } 88 | 89 | return nil 90 | } 91 | -------------------------------------------------------------------------------- /cmd/init.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/yashishdua/gpm/internal" 7 | ) 8 | 9 | func Init() { 10 | internal.PrintDescribe("Initializing gpm...") 11 | 12 | initScript := `mkdir -p .gpm` 13 | if scriptErr := internal.ConfigureScript(initScript).Run(); scriptErr != nil { 14 | fmt.Println(scriptErr) 15 | return 16 | } 17 | 18 | internal.PrintStep("Initialized") 19 | } 20 | -------------------------------------------------------------------------------- /cmd/main.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "strings" 7 | 8 | "github.com/yashishdua/gpm/internal" 9 | 10 | "github.com/spf13/cobra" 11 | "github.com/spf13/pflag" 12 | ) 13 | 14 | var internalFlags internal.Flags 15 | 16 | func Exec() { 17 | var rootCmd = &cobra.Command{Use: "gpm"} 18 | 19 | var versionCmd = &cobra.Command{ 20 | Use: "version", 21 | Short: "Print the version number of gpm", 22 | Long: `All software has versions. This is gpm's`, 23 | Run: func(cmd *cobra.Command, args []string) { 24 | fmt.Println("gpm - Go Project Manager v0.0.1") 25 | }, 26 | } 27 | 28 | var initCmd = &cobra.Command{ 29 | Use: "init", 30 | Short: "Initializes the project", 31 | Long: `Initializes the project`, 32 | Run: func(cmd *cobra.Command, args []string) { 33 | if isFileExist, _ := internal.CheckFileExist(".gpm"); isFileExist { 34 | internal.PrintStep("gpm already initialized") 35 | } else { 36 | Init() 37 | } 38 | }, 39 | } 40 | 41 | var createCmd = &cobra.Command{ 42 | Use: "create", 43 | Short: "Creates directory structure", 44 | Long: `Create the recommended project directory structure`, 45 | Run: func(cmd *cobra.Command, args []string) { 46 | if preCheck() { 47 | SetupProject() 48 | } 49 | }, 50 | } 51 | 52 | var modCmd = &cobra.Command{ 53 | Use: "mod", 54 | Short: "Creates modules file", 55 | Long: `Creates modules file`, 56 | Run: func(cmd *cobra.Command, args []string) { 57 | if preCheck() { 58 | SetupMod() 59 | } 60 | }, 61 | } 62 | 63 | var vendorCmd = &cobra.Command{ 64 | Use: "vendor", 65 | Short: "Creates vendor using modules", 66 | Long: `Creates vendor using modules`, 67 | Run: func(cmd *cobra.Command, args []string) { 68 | if preCheck() { 69 | SetupVendor() 70 | } 71 | }, 72 | } 73 | 74 | var updateCmd = &cobra.Command{ 75 | Use: "update", 76 | Short: "Updates Go version", 77 | Long: `Updates Go version to a entered version`, 78 | PreRunE: func(cmd *cobra.Command, args []string) error { 79 | flags := cmd.Flags() 80 | version := "" 81 | flags.VisitAll(func(flag *pflag.Flag) { 82 | if flag.Shorthand == "v" && flag.Changed { 83 | version = flag.Value.String() 84 | } 85 | }) 86 | 87 | if strings.Contains(version, "go") { 88 | return errors.New("version cannot contain 'go' keyword") 89 | } 90 | 91 | return nil 92 | }, 93 | Run: func(cmd *cobra.Command, args []string) { 94 | UpdateVersion(internalFlags) 95 | }, 96 | } 97 | 98 | updateCmd.Flags().StringVarP(&internalFlags.Version, "version", "v", "", "Version Number") 99 | 100 | var buildCmd = &cobra.Command{ 101 | Use: "build", 102 | Short: "Build the project", 103 | Long: `Helps building project using mod and vendor, and works inside and outside the GOPATH`, 104 | PreRunE: func(cmd *cobra.Command, args []string) error { 105 | flags := cmd.Flags() 106 | flagPresent := false 107 | flags.VisitAll(func(flag *pflag.Flag) { 108 | if flag.Shorthand == "v" && flag.Changed { 109 | flagPresent = true 110 | } else if flag.Shorthand == "m" && flag.Changed { 111 | flagPresent = true 112 | } 113 | }) 114 | 115 | if !flagPresent { 116 | return errors.New("build type required (vendor or modules)") 117 | } 118 | return nil 119 | }, 120 | Run: func(cmd *cobra.Command, args []string) { 121 | if preCheck() { 122 | Build(internalFlags) 123 | } 124 | }, 125 | } 126 | 127 | buildCmd.Flags().BoolVarP(&internalFlags.Vendor, "vendor", "v", false, "Builds project using vendor") 128 | buildCmd.Flags().BoolVarP(&internalFlags.Modules, "modules", "m", false, "Builds project using modules") 129 | 130 | rootCmd.AddCommand(versionCmd) 131 | rootCmd.AddCommand(initCmd) 132 | rootCmd.AddCommand(buildCmd) 133 | rootCmd.AddCommand(createCmd) 134 | rootCmd.AddCommand(updateCmd) 135 | rootCmd.AddCommand(modCmd) 136 | rootCmd.AddCommand(vendorCmd) 137 | 138 | if err := rootCmd.Execute(); err != nil { 139 | internal.PrintError(err) 140 | } 141 | } 142 | 143 | func preCheck() bool { 144 | isFileExist, _ := internal.CheckFileExist(".gpm") 145 | 146 | if !isFileExist { 147 | internal.PrintStep("gpm not initialized") 148 | internal.PrintStep("Use to initialize the project") 149 | return false 150 | } 151 | 152 | return true 153 | } 154 | -------------------------------------------------------------------------------- /cmd/mod.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | 8 | "github.com/yashishdua/gpm/internal" 9 | ) 10 | 11 | func SetupMod() { 12 | internal.PrintDescribe("Creating modules file...") 13 | 14 | if isFileExist, _ := internal.CheckFileExist("go.mod"); isFileExist { 15 | internal.PrintStep("Modules file already exist") 16 | return 17 | } 18 | 19 | reader := bufio.NewReader(os.Stdin) 20 | internal.PrintStep("Enter module name: ") 21 | text, _ := reader.ReadString('\n') 22 | modScript := fmt.Sprintf(`go mod init %s`, text) 23 | 24 | // Check if inside GOPATH 25 | dir, dirErr := internal.GetCurrentDir() 26 | if dirErr != nil { 27 | internal.PrintError(dirErr) 28 | return 29 | } 30 | 31 | if insideGoPath := internal.CheckInsideGoPath(dir); insideGoPath { 32 | modScript = fmt.Sprintf(`GO111MODULE=on %s`, modScript) 33 | } 34 | 35 | if scriptErr := internal.ConfigureScript(modScript).Run(); scriptErr != nil { 36 | internal.PrintError(scriptErr) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /cmd/update.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/yashishdua/gpm/internal" 7 | ) 8 | 9 | func UpdateVersion(internalFlags internal.Flags) { 10 | internal.PrintDescribe("Updating Go version...") 11 | 12 | if len(internalFlags.Version) <= 0 { // Default Version 13 | internal.PrintStep("Using default version 1.12.5") 14 | internalFlags.Version = "1.12.5" 15 | } 16 | 17 | goBinaryFile := fmt.Sprintf(`go%s.darwin-amd64.tar.gz`, internalFlags.Version) 18 | downloadURL := fmt.Sprintf(`https://dl.google.com/go/%s`, goBinaryFile) 19 | uninstallScript := `sudo rm -rf /usr/local/go` 20 | extractScript := fmt.Sprintf(`sudo tar -C /usr/local -xzf %s`, goBinaryFile) 21 | removeBinaryScript := fmt.Sprintf(`sudo rm %s`, goBinaryFile) 22 | 23 | internal.PrintStep("Uninstalling previous version") 24 | if scriptErr := internal.ConfigureScript(uninstallScript).Run(); scriptErr != nil { 25 | internal.PrintError(scriptErr) 26 | return 27 | } 28 | 29 | internal.PrintStep(fmt.Sprintf(`Downloading %s binary`, goBinaryFile)) 30 | if fileExist, _ := internal.CheckFileExist(goBinaryFile); fileExist { 31 | internal.PrintStep("Go binary file already exist") 32 | } else { 33 | downloadErr := internal.DownloadFile(goBinaryFile, downloadURL) 34 | if downloadErr != nil { 35 | internal.PrintError(downloadErr) 36 | internal.PrintStep("Go Server error or Check version entered once") 37 | return 38 | } 39 | } 40 | 41 | internal.PrintStep("Extracting Go archive") 42 | if scriptErr := internal.ConfigureScript(extractScript).Run(); scriptErr != nil { 43 | internal.PrintError(scriptErr) 44 | return 45 | } 46 | 47 | if scriptErr := internal.ConfigureScript(removeBinaryScript).Run(); scriptErr != nil { 48 | internal.PrintError(scriptErr) 49 | return 50 | } 51 | 52 | internal.PrintStep("Go updated successfuly") 53 | } 54 | -------------------------------------------------------------------------------- /cmd/vendor.go: -------------------------------------------------------------------------------- 1 | package cmd 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/yashishdua/gpm/internal" 7 | ) 8 | 9 | func SetupVendor() { 10 | internal.PrintDescribe("Creating vendor...") 11 | 12 | var vendorScript = `go mod vendor` 13 | 14 | dir, dirErr := internal.GetCurrentDir() 15 | if dirErr != nil { 16 | internal.PrintError(dirErr) 17 | return 18 | } 19 | 20 | if insideGoPath := internal.CheckInsideGoPath(dir); insideGoPath { 21 | vendorScript = fmt.Sprintf(`GO111MODULE=on %s`, vendorScript) 22 | } 23 | 24 | internal.PrintStep("using modules to build vendor") 25 | if scriptErr := internal.ConfigureScript(vendorScript).Run(); scriptErr != nil { 26 | internal.PrintError(scriptErr) 27 | return 28 | } 29 | 30 | internal.PrintStep("Vendor created") 31 | } 32 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/yashishdua/gpm 2 | 3 | go 1.12 4 | 5 | require ( 6 | github.com/fatih/color v1.7.0 7 | github.com/mattn/go-colorable v0.1.1 // indirect 8 | github.com/mattn/go-isatty v0.0.7 // indirect 9 | github.com/spf13/cobra v0.0.3 10 | github.com/spf13/pflag v1.0.3 11 | ) 12 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= 2 | github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= 3 | github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg= 4 | github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= 5 | github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= 6 | github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc= 7 | github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= 8 | github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8= 9 | github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= 10 | github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= 11 | github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= 12 | golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 13 | -------------------------------------------------------------------------------- /internal/interfaces.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | type Flags struct { 4 | Version string `json:"path"` 5 | Vendor bool `json:"vendor"` 6 | Modules bool `json:"modules"` 7 | } 8 | -------------------------------------------------------------------------------- /internal/logger.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "fmt" 5 | "github.com/fatih/color" 6 | ) 7 | 8 | func PrintDescribe(text string) { 9 | text = fmt.Sprintf(`# %s`, text) 10 | fmt.Println(text) 11 | } 12 | 13 | func PrintStep(text string) { 14 | text = fmt.Sprintf(`gpm: %s`, text) 15 | fmt.Println(text) 16 | } 17 | 18 | func PrintError(err error) { 19 | text := fmt.Sprintf(`gpm: [ERROR] %s`, err.Error()) 20 | color.Red(text) 21 | } 22 | -------------------------------------------------------------------------------- /internal/utils.go: -------------------------------------------------------------------------------- 1 | package internal 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "io" 7 | "net/http" 8 | "os" 9 | "os/exec" 10 | "strings" 11 | ) 12 | 13 | func CheckFileExist(fileName string) (bool, error) { 14 | if _, err := os.Stat(fileName); err == nil { 15 | // path/to/whatever exists 16 | return true, nil 17 | } else if os.IsNotExist(err) { 18 | // path/to/whatever does *not* exist 19 | return false, nil 20 | } 21 | return false, errors.New("Error checking file") 22 | } 23 | 24 | func GetCurrentDir() (string, error) { 25 | dir, err := os.Getwd() 26 | if err != nil { 27 | return "", err 28 | } 29 | return dir, nil 30 | } 31 | 32 | func CheckInsideGoPath(dir string) bool { 33 | if strings.Contains(dir, os.Getenv("GOPATH")) { 34 | dir = dir[len(os.Getenv("GOPATH")):] 35 | if fmt.Sprintf("%c", dir[1]) == "/" { 36 | return true 37 | } 38 | } 39 | return false 40 | } 41 | 42 | func CheckGoVersion() (string, error) { 43 | out, err := exec.Command("/bin/sh", "-c", "go version").Output() 44 | if err != nil { 45 | return "", err 46 | } 47 | words := strings.Fields(string(out)) 48 | return words[2], nil 49 | } 50 | 51 | func GetFileContentType(out *os.File) (string, error) { 52 | // Only the first 512 bytes are used to sniff the content type. 53 | buffer := make([]byte, 512) 54 | 55 | _, err := out.Read(buffer) 56 | if err != nil { 57 | return "", err 58 | } 59 | 60 | // Use the net/http package's handy DectectContentType function. Always returns a valid 61 | // content-type by returning "application/octet-stream" if no others seemed to match. 62 | contentType := http.DetectContentType(buffer) 63 | return contentType, nil 64 | } 65 | 66 | func ConfigureScript(script string) *exec.Cmd { 67 | cmd := exec.Command("/bin/sh", "-c", script) 68 | cmd.Stdout = os.Stdout 69 | cmd.Stderr = os.Stderr 70 | return cmd 71 | } 72 | 73 | func DownloadFile(filepath string, url string) error { 74 | // Create the file 75 | out, createErr := os.Create(filepath) 76 | if createErr != nil { 77 | return createErr 78 | } 79 | 80 | defer out.Close() 81 | 82 | // Get the data 83 | resp, httpErr := http.Get(url) 84 | if httpErr != nil { 85 | return httpErr 86 | } 87 | 88 | defer resp.Body.Close() 89 | 90 | // Check server response 91 | if resp.StatusCode != http.StatusOK { 92 | return fmt.Errorf("Bad status from golang.org: %s", resp.Status) 93 | } 94 | 95 | // Writer the body to file 96 | _, err := io.Copy(out, resp.Body) 97 | if err != nil { 98 | return err 99 | } 100 | 101 | return nil 102 | } 103 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | cmd "github.com/yashishdua/gpm/cmd" 5 | ) 6 | 7 | func main() { 8 | cmd.Exec() 9 | } 10 | -------------------------------------------------------------------------------- /static/gpm.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YashishDua/gpm/9ff038ffb1ed5ff11d8d6e18a515e7e70af44a74/static/gpm.gif -------------------------------------------------------------------------------- /static/gpm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YashishDua/gpm/9ff038ffb1ed5ff11d8d6e18a515e7e70af44a74/static/gpm.png --------------------------------------------------------------------------------