-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
bc508d9
commit 8a1da1a
Showing
12 changed files
with
450 additions
and
451 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 |
---|---|---|
@@ -1,72 +1,72 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"errors" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/yashishdua/gpm/internal" | ||
"github.com/yashishdua/gpm/internal" | ||
) | ||
|
||
func Build(flags internal.Flags) { | ||
internal.PrintDescribe("Building...") | ||
internal.PrintDescribe("Building...") | ||
|
||
buildScript := getBuildScript(flags.Vendor, flags.Modules) | ||
if buildScript == "" { | ||
internal.PrintError(errors.New("Build failed")) | ||
return | ||
} | ||
buildScript := getBuildScript(flags.Vendor, flags.Modules) | ||
if buildScript == "" { | ||
internal.PrintError(errors.New("Build failed")) | ||
return | ||
} | ||
|
||
if scriptErr := internal.ConfigureScript(buildScript).Run(); scriptErr != nil { | ||
internal.PrintError(scriptErr) | ||
return | ||
} | ||
if scriptErr := internal.ConfigureScript(buildScript).Run(); scriptErr != nil { | ||
internal.PrintError(scriptErr) | ||
return | ||
} | ||
|
||
internal.PrintStep("Build successful") | ||
internal.PrintStep("Build successful") | ||
} | ||
|
||
func getBuildScript(vendorFlag bool, modFlag bool) string { | ||
buildScript := `go build` | ||
|
||
if modFlag { | ||
if modExist, _ := internal.CheckFileExist("go.mod"); modExist { | ||
dir, dirErr := internal.GetCurrentDir() | ||
if dirErr != nil { | ||
internal.PrintError(dirErr) | ||
return "" | ||
} | ||
|
||
if insideGoPath := internal.CheckInsideGoPath(dir); insideGoPath { // Inside GOPATH | ||
internal.PrintStep("Using modules to build inside GOPATH") | ||
buildScript = fmt.Sprintf(`GO111MODULE=on %s`, buildScript) | ||
} | ||
|
||
return buildScript | ||
} | ||
|
||
internal.PrintStep("modules doesn't exist") | ||
return "" | ||
} | ||
|
||
if vendorFlag { | ||
if vendorExist, _ := internal.CheckFileExist("vendor"); vendorExist { | ||
dir, dirErr := internal.GetCurrentDir() | ||
if dirErr != nil { | ||
internal.PrintError(dirErr) | ||
return "" | ||
} | ||
|
||
if insideGoPath := internal.CheckInsideGoPath(dir); insideGoPath { // Inside GOPATH | ||
internal.PrintStep("Using vendor to build inside GOPATH") | ||
buildScript = fmt.Sprintf(`GO111MODULE=on %s -mod=vendor`, buildScript) | ||
} | ||
|
||
return buildScript | ||
} | ||
|
||
internal.PrintStep("vendor doesn't exist") | ||
return "" | ||
} | ||
|
||
internal.PrintStep("no vendor or modules were present") | ||
return "" | ||
buildScript := `go build` | ||
|
||
if modFlag { | ||
if modExist, _ := internal.CheckFileExist("go.mod"); modExist { | ||
dir, dirErr := internal.GetCurrentDir() | ||
if dirErr != nil { | ||
internal.PrintError(dirErr) | ||
return "" | ||
} | ||
|
||
if insideGoPath := internal.CheckInsideGoPath(dir); insideGoPath { // Inside GOPATH | ||
internal.PrintStep("Using modules to build inside GOPATH") | ||
buildScript = fmt.Sprintf(`GO111MODULE=on %s`, buildScript) | ||
} | ||
|
||
return buildScript | ||
} | ||
|
||
internal.PrintStep("modules doesn't exist") | ||
return "" | ||
} | ||
|
||
if vendorFlag { | ||
if vendorExist, _ := internal.CheckFileExist("vendor"); vendorExist { | ||
dir, dirErr := internal.GetCurrentDir() | ||
if dirErr != nil { | ||
internal.PrintError(dirErr) | ||
return "" | ||
} | ||
|
||
if insideGoPath := internal.CheckInsideGoPath(dir); insideGoPath { // Inside GOPATH | ||
internal.PrintStep("Using vendor to build inside GOPATH") | ||
buildScript = fmt.Sprintf(`GO111MODULE=on %s -mod=vendor`, buildScript) | ||
} | ||
|
||
return buildScript | ||
} | ||
|
||
internal.PrintStep("vendor doesn't exist") | ||
return "" | ||
} | ||
|
||
internal.PrintStep("no vendor or modules were present") | ||
return "" | ||
} |
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,71 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os/exec" | ||
"strconv" | ||
|
||
"github.com/yashishdua/gpm/internal" | ||
) | ||
|
||
// Array cannot me made constant in Go | ||
var dirs = []string{"cmd", "internal", "pkg", "scripts", "api", "test"} | ||
|
||
func SetupProject() { | ||
internal.PrintDescribe("Setting up project structure...") | ||
|
||
for _, dir := range dirs { | ||
internal.PrintStep("Creating " + dir + " directory") | ||
if err := execSetupScript(dir); err != nil { | ||
internal.PrintError(err) | ||
return | ||
} | ||
} | ||
|
||
internal.PrintStep("Create successful") | ||
} | ||
|
||
func execSetupScript(dir string) error { | ||
scripts := getScripts(dir) | ||
script, countScript, keepScript := scripts[0], scripts[1], scripts[2] | ||
|
||
if _, scriptErr := exec.Command("/bin/sh", "-c", script).Output(); scriptErr != nil { | ||
return scriptErr | ||
} | ||
|
||
cmd := exec.Command("/bin/sh", "-c", countScript) | ||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
scanner := bufio.NewScanner(stdout) | ||
scanner.Split(bufio.ScanWords) | ||
for scanner.Scan() { | ||
value, _ := strconv.Atoi(scanner.Text()) | ||
if value == 0 { | ||
if _, scriptErr := exec.Command("/bin/sh", "-c", keepScript).Output(); scriptErr != nil { | ||
return scriptErr | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getScripts(dir string) []string { | ||
script := fmt.Sprintf(`mkdir -p %s`, dir) | ||
countScript := fmt.Sprintf(`cd %s && ls | wc -l`, dir) | ||
keepScript := fmt.Sprintf(`cd %s && touch .keep`, dir) | ||
return []string{script, countScript, keepScript} | ||
} |
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 |
---|---|---|
@@ -1,19 +1,19 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"fmt" | ||
|
||
"github.com/yashishdua/gpm/internal" | ||
"github.com/yashishdua/gpm/internal" | ||
) | ||
|
||
func Init() { | ||
internal.PrintDescribe("Initializing gpm...") | ||
func Init() { | ||
internal.PrintDescribe("Initializing gpm...") | ||
|
||
initScript := `mkdir -p .gpm` | ||
if scriptErr := internal.ConfigureScript(initScript).Run(); scriptErr != nil { | ||
fmt.Println(scriptErr) | ||
return | ||
} | ||
initScript := `mkdir -p .gpm` | ||
if scriptErr := internal.ConfigureScript(initScript).Run(); scriptErr != nil { | ||
fmt.Println(scriptErr) | ||
return | ||
} | ||
|
||
internal.PrintStep("Initialized") | ||
internal.PrintStep("Initialized") | ||
} |
Oops, something went wrong.