Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Jason McCallister <[email protected]>
  • Loading branch information
jasonmccallister committed Apr 15, 2020
1 parent 64a22b0 commit a008ef3
Showing 1 changed file with 31 additions and 40 deletions.
71 changes: 31 additions & 40 deletions internal/cmd/self-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,50 @@ package cmd

import (
"fmt"
"github.com/spf13/cobra"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
)

var (
selfUpdateCommand = &cobra.Command{
Use: "self-update",
Short: "Update Nitro to the latest",
RunE: func(cmd *cobra.Command, args []string) error {
fileUrl := "https://raw.githubusercontent.com/pixelandtonic/nitro/develop/get.sh"
"github.com/spf13/cobra"
)

tempFolder := os.TempDir()
var selfUpdateCommand = &cobra.Command{
Use: "self-update",
Short: "Update Nitro to the latest",
RunE: func(cmd *cobra.Command, args []string) error {
fileUrl := "https://raw.githubusercontent.com/pixelandtonic/nitro/develop/get.sh"

localFile := filepath.Join(tempFolder, "get.sh")
tempFolder := os.TempDir()
defer os.Remove(tempFolder)

if downloadErr := DownloadFile(localFile, fileUrl); downloadErr != nil {
panic(downloadErr)
}
localFile := filepath.Join(tempFolder, "get.sh")

permErr := os.Chmod(localFile, 0777)
if permErr != nil {
log.Println(permErr)
}
if err := DownloadFile(localFile, fileUrl); err != nil {
return err
}

ch := make(chan string)
go func() {
err := RunCommandCh(ch, "\r\n", localFile)
if err != nil {
log.Fatal(err)
}
}()
if err := os.Chmod(localFile, 0777); err != nil {
return err
}

for v := range ch {
fmt.Println(v)
ch := make(chan string)
go func() {
if err := RunCommandCh(ch, "\r\n", localFile); err != nil {
log.Fatal(err)
}
}()

for v := range ch {
fmt.Println(v)
}

defer os.Remove(tempFolder)

return nil
},
}
)
return nil
},
}

func DownloadFile(filepath string, url string) error {

Expand All @@ -73,18 +68,17 @@ func DownloadFile(filepath string, url string) error {
return err
}


// RunCommandCh runs an arbitrary command and streams the output to a channel.
func RunCommandCh(stdoutCh chan<- string, cutset string, command string, flags ...string) error {
cmd := exec.Command(command, flags...)

output, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("RunCommand: cmd.StdoutPipe(): %v", err)
return err
}

if err := cmd.Start(); err != nil {
return fmt.Errorf("RunCommand: cmd.Start(): %v", err)
return err
}

go func() {
Expand Down Expand Up @@ -123,8 +117,5 @@ func RunCommandCh(stdoutCh chan<- string, cutset string, command string, flags .
}
}()

if err := cmd.Wait(); err != nil {
return fmt.Errorf("RunCommand: cmd.Wait(): %v", err)
}
return nil
}
return cmd.Wait()
}

0 comments on commit a008ef3

Please sign in to comment.