Skip to content

Commit

Permalink
#updated install cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-hermann-sva committed Nov 26, 2024
1 parent 7e11e99 commit 63ea544
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
21 changes: 16 additions & 5 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var installCmd = &cobra.Command{
gitPath, _ := cmd.LocalFlags().GetString("path")
profile, _ := cmd.LocalFlags().GetString("profile")
source, _ := cmd.LocalFlags().GetString("source")
url, _ := cmd.LocalFlags().GetString("url")
bin, _ := cmd.LocalFlags().GetString("bin")
tmpDownloadDir, _ := cmd.LocalFlags().GetString("tmp")
binaries, _ := cmd.Flags().GetStringSlice("binaries")
Expand All @@ -37,14 +38,23 @@ var installCmd = &cobra.Command{
internal.PrintBanner(logFilePath, gitPath, gitRepository, version, date, "/INSTALL")

// LOAD PROFILE BASED ON SOURCE
if source == "git" {
switch source {

case "fetch":
content, err := internal.DownloadFile(url)
profileFile = content
if err != nil {
log.Error("ERROR BY DOWNLOADING: ", err)
os.Exit(3)
}

case "git":
// GET REPO + READ PROFILE FILE
repo, _ := sthingsCli.CloneGitRepository(gitRepository, gitBranch, gitCommitID, nil)
profileFile = sthingsCli.ReadFileContentFromGitRepo(repo, profile)

} else if source == "local" {

case "local":
// GET LOCAL FILE
profileExists, _ := sthingsBase.VerifyIfFileOrDirExists(profile, "file")
log.Info("LOCAL PROFILE FOUND : ", profile)

Expand All @@ -55,7 +65,7 @@ var installCmd = &cobra.Command{
os.Exit(3)
}

} else {
default:
log.Error("SOURCE: GIT OR LOCAL ONLY", source)
os.Exit(3)
}
Expand Down Expand Up @@ -94,7 +104,8 @@ func init() {
rootCmd.AddCommand(installCmd)
installCmd.Flags().String("tmp", "/tmp/machineShop", "temporary machineShop dir")
installCmd.Flags().String("bin", "/usr/bin", "target dir for installing binary files")
installCmd.Flags().String("source", "git", "source of profile: git or local")
installCmd.Flags().String("source", "fetch", "source of profile: local, git or by url fetch. default: fetch.")
installCmd.Flags().String("url", "https://raw.githubusercontent.com/stuttgart-things/stuttgart-things/refs/heads/main/machineShop/binaries.yaml", "source of url download")
installCmd.Flags().String("profile", "machineShop/binaries.yaml", "path to install profile")
installCmd.Flags().StringSlice("binaries", []string{}, "files to be installed; survey will be skipped if defined")
}
37 changes: 37 additions & 0 deletions internal/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright © 2023 Patrick Hermann [email protected]
*/

package internal

import (
"io"
"net/http"
"os"
)

func DownloadFile(url string) (string, error) {

// HTTP GET REQUEST
resp, err := http.Get(url)
if err != nil {
log.Error("ERROR DOWNLOADING THE FILE: ", err)
os.Exit(3)
}
defer resp.Body.Close()

// CHECK HTTP STATUS
if resp.StatusCode != http.StatusOK {
log.Error("ERROR DOWNLOADING THE FILE: STATUS ", resp.StatusCode)
os.Exit(3)
}

// READ ANSWER
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Error("ERROR DOWNLOADING THE FILE: ", err)
os.Exit(3)
}

return string(body), err
}
33 changes: 33 additions & 0 deletions internal/download_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package internal

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)

func TestDownloadFile(t *testing.T) {
// Create a test HTTP server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Simulate a successful response
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("Test content of the file"))
if err != nil {
fmt.Println("ERROR BY WRITING RESPONSE: ", err)
}
}))
defer ts.Close()

// Call the DownloadFile function with the test URL
content, err := DownloadFile(ts.URL)
if err != nil {
t.Fatalf("DownloadFile returned an error: %v", err)
}

// Verify the content of the downloaded file
expected := "Test content of the file"
if content != expected {
t.Errorf("Expected '%s', but got '%s'", expected, content)
}
}

0 comments on commit 63ea544

Please sign in to comment.