Skip to content

Commit

Permalink
fix update bug, catch mod init with bad module name
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm committed May 31, 2020
1 parent a279f4e commit 0d42781
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 27 deletions.
26 changes: 17 additions & 9 deletions .hof/shadow/Cli/cmd/hof/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ func downloadAndInstall(url string) error {
req = req.SetBasicAuth("github-token", os.Getenv("GITHUB_TOKEN"))
}

// handle redirects
req.RedirectPolicy(func(r gorequest.Request, via []gorequest.Request) error {
viaURL := []string{}
for _, v := range via {
viaURL = append(viaURL, v.URL.String())
}
return nil
})

resp, content, errs := req.Get(url).EndBytes()

check := "http2: server sent GOAWAY and closed the connection"
Expand Down Expand Up @@ -337,16 +346,15 @@ func downloadAndInstall(url string) error {
}

// Sudo copy the file
cmd := exec.Command("/bin/sh", "-c",
fmt.Sprintf("export OWNER=$(ls -l %s | awk '{ print $3 \":\" $4 }') && sudo mv %s %s-v%s && sudo cp %s %s && sudo chown $OWNER %s && sudo chmod 0755 %s",
real, // get owner
real, real, verinfo.Version, // backup
tmpfile.Name(), real, // cp
real, // chown
real, // chmod
real, // rm
),
cmdStr := fmt.Sprintf("export OWNER=$(ls -l %s | awk '{ print $3 \":\" $4 }') && sudo mv %s %s-v%s && sudo cp %s %s && sudo chown $OWNER %s && sudo chmod 0755 %s",
real, // get owner
real, real, verinfo.Version, // backup
tmpfile.Name(), real, // cp
real, // chown
real, // chmod
)
// fmt.Println("CMDSTR:", cmdStr)
cmd := exec.Command("/bin/sh", "-c", cmdStr)

// prep stdin for password
stdin, err := cmd.StdinPipe()
Expand Down
26 changes: 17 additions & 9 deletions cmd/hof/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ func downloadAndInstall(url string) error {
req = req.SetBasicAuth("github-token", os.Getenv("GITHUB_TOKEN"))
}

// handle redirects
req.RedirectPolicy(func(r gorequest.Request, via []gorequest.Request) error {
viaURL := []string{}
for _, v := range via {
viaURL = append(viaURL, v.URL.String())
}
return nil
})

resp, content, errs := req.Get(url).EndBytes()

check := "http2: server sent GOAWAY and closed the connection"
Expand Down Expand Up @@ -337,16 +346,15 @@ func downloadAndInstall(url string) error {
}

// Sudo copy the file
cmd := exec.Command("/bin/sh", "-c",
fmt.Sprintf("export OWNER=$(ls -l %s | awk '{ print $3 \":\" $4 }') && sudo mv %s %s-v%s && sudo cp %s %s && sudo chown $OWNER %s && sudo chmod 0755 %s",
real, // get owner
real, real, verinfo.Version, // backup
tmpfile.Name(), real, // cp
real, // chown
real, // chmod
real, // rm
),
cmdStr := fmt.Sprintf("export OWNER=$(ls -l %s | awk '{ print $3 \":\" $4 }') && sudo mv %s %s-v%s && sudo cp %s %s && sudo chown $OWNER %s && sudo chmod 0755 %s",
real, // get owner
real, real, verinfo.Version, // backup
tmpfile.Name(), real, // cp
real, // chown
real, // chmod
)
// fmt.Println("CMDSTR:", cmdStr)
cmd := exec.Command("/bin/sh", "-c", cmdStr)

// prep stdin for password
stdin, err := cmd.StdinPipe()
Expand Down
2 changes: 1 addition & 1 deletion cue.mods
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/hofstadter-io/hof
cue master

require (
github.com/hofstadter-io/hofmod-cli v0.5.6
github.com/hofstadter-io/hofmod-cli v0.5.9
github.com/hofstadter-io/hofmod-cuefig v0.0.1
)

Expand Down
9 changes: 9 additions & 0 deletions lib/mod/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ import (
"github.com/hofstadter-io/hof/lib/yagu"
)

func envSetup(env *testscript.Env) error {

env.Vars = append(env.Vars, "HOF_TELEMETRY_DISABLED=1")

return nil
}

func TestMod(t *testing.T) {

yagu.Mkdir(".workdir/tests")

testscript.Run(t, testscript.Params{
Setup: envSetup,
Dir: "testdata",
WorkdirRoot: ".workdir/tests",
})
Expand All @@ -23,6 +31,7 @@ func TestModBugs(t *testing.T) {
yagu.Mkdir(".workdir/bugs")

testscript.Run(t, testscript.Params{
Setup: envSetup,
Dir: "testdata/bugs",
WorkdirRoot: ".workdir/bugs",
})
Expand Down
9 changes: 8 additions & 1 deletion lib/mod/modder/modder_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path"
"text/template"

gomod "golang.org/x/mod/module"

"github.com/hofstadter-io/hof/lib/mod/parse/modfile"
"github.com/hofstadter-io/hof/lib/mod/util"
)
Expand Down Expand Up @@ -46,8 +48,13 @@ func (mdr *Modder) initModFile(module string) error {
lang := mdr.Name
filename := mdr.ModFile

err := gomod.CheckPath(module)
if err != nil {
return fmt.Errorf("bad module format %q, should be 'domain.com/repo/proj'", module)
}

// make sure file does not exist
_, err := ioutil.ReadFile(filename)
_, err = ioutil.ReadFile(filename)
// we read the file and it exists
if err == nil {
return fmt.Errorf("%s already exists", filename)
Expand Down
4 changes: 0 additions & 4 deletions lib/mod/testdata/bugs/init__module_badfmt_not_caught.txt

This file was deleted.

9 changes: 6 additions & 3 deletions lib/mod/testdata/init__arg_errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ stdout 'Unknown language "blah".'
! stderr '.'


# hof mod init - known lan, badfmt module
! exec hof mod init cue blah
stdout 'bad module format "blah", should be ''domain.com/repo/proj'''
! stderr '.'


# hof mod init - backwards args
! exec hof mod init github.com/test/backwards cue
stdout 'Unknown language "github.com/test/backwards".'
! stderr .




# hof mod init - ensure none of these created any files

! exists cue.mods
! exists cue.sums
! exists cue.mod/
Expand Down

0 comments on commit 0d42781

Please sign in to comment.