Skip to content

Commit

Permalink
Upgraded to 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharpz7 committed Aug 17, 2023
1 parent d398d5b commit ded38f3
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
.env
/env
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION=1.7
VERSION=1.8
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ scripts:
# Installation
On linux, just run:
```console
sudo curl -s -L https://github.com/SharpSet/sharpdev/releases/download/1.7/install.sh | sudo bash
sudo curl -s -L https://github.com/SharpSet/sharpdev/releases/download/1.8/install.sh | sudo bash
```

## Command Options
Expand Down
14 changes: 12 additions & 2 deletions sharpdev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ version: 1.0
envfile: .env
values:
TEST: Values work
_ROOT: /home/coder/code-server/go/src/sharpdev
sharpdev_test: /home/coder/code-server/go/src/sharpdev/internal/sharpdev
_ROOT: /home/coder/projects/sharpdev
sharpdev_test: /home/coder/projects/sharpdev/internal/sharpdev

setup: |
go build -o _ROOT/internal/sharpdev _ROOT/src
Expand Down Expand Up @@ -46,6 +46,15 @@ scripts:
sharpdev_test echo5 yay! Duplicates_Work!
sharpdev_test echo2 Extra Args Works!
test_env_download: |
sharpdev_test --url=https://github.com/Sharpz7/dotfiles --envname=k8s
if [ -f ./env/sharpdev.yml ]; then
echo "File Exists"
else
echo "File Does Not Exist"
fi
echo1: echo TEST
echo2: echo "$_ARG1 $_ARG2"
echo3: echo "Env and Parent ${ECHO:-failed}"
Expand All @@ -62,3 +71,4 @@ scripts:
sharpdev test_env_sub
sharpdev test_setup
sharpdev_test -ss test_skip_setup
sharpdev_test test_env_download
54 changes: 54 additions & 0 deletions src/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
)

func downloadDotFile(dotfileURL, envName string) error {
// Convert GitHub repo URL to the raw content URL
rawURL := strings.Replace(dotfileURL, "github.com", "raw.githubusercontent.com", 1)
rawURL = strings.Replace(rawURL, "/blob", "", 1)

// Construct the URL to the sharpdev.yml in the repository
fileURL := fmt.Sprintf("%s/main/envs/%s/sharpdev.yml", rawURL, envName)

// Send GET request
resp, err := http.Get(fileURL)
if err != nil {
return err
}
defer resp.Body.Close()

// Check the status code
if resp.StatusCode != 200 {
return errors.New("Failed to fetch the file, status: " + resp.Status + ": " + fileURL)
}

// Ensure the destination directory exists
destDir := "./env"
if err := os.MkdirAll(destDir, 0o755); err != nil {
return err
}

// Create the destination file
destPath := filepath.Join(destDir, "sharpdev.yml")
out, err := os.Create(destPath)
if err != nil {
return err
}
defer out.Close()

// Write the response body to the file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}

return nil
}
7 changes: 4 additions & 3 deletions src/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func genFile() {
Scripts: scriptsEx,
Setup: "echo 'Setup command goes here'",
Values: values,
EnvFile: ".env"}
EnvFile: ".env",
}

err := saveFile(testfile)
check(err, "Failed to generate sharpdev.yml")
Expand All @@ -43,7 +44,7 @@ func loadFile(parent *bool) config {
var err error
var dir string = "./"

if *parent == true {
if *parent {
// find the parent directory
dir, err = os.Getwd()
check(err, "Failed to get current directory")
Expand Down Expand Up @@ -124,7 +125,7 @@ func saveFile(devFile config) error {
// convert string to bytes
yamlBytes := []byte(yamlString)

writeErr := ioutil.WriteFile("./sharpdev.yml", yamlBytes, 0644)
writeErr := ioutil.WriteFile("./sharpdev.yml", yamlBytes, 0o644)

if marshErr != nil || writeErr != nil {
return errors.New("failed to save file")
Expand Down
32 changes: 18 additions & 14 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import (
)

// add a -p flag var as bool
var parent = flag.Bool("p", false, "Use a parent sharpdev.yml file")
var version = flag.Bool("v", false, "Get the version number")
var version2 = flag.Bool("version", false, "Get the version number")
var skipSetup = flag.Bool("ss", false, "Skips using the setup option")
var (
parent = flag.Bool("p", false, "Use a parent sharpdev.yml file")
version = flag.Bool("v", false, "Get the version number")
version2 = flag.Bool("version", false, "Get the version number")
skipSetup = flag.Bool("ss", false, "Skips using the setup option")
dotFile = flag.String("url", "", "The dotfile repo for the sharpdev files. See https://github.com/Sharpz7/dotfiles")
envName = flag.String("envname", "", "The name of sharpdev env to download from")
)

func main() {
var name string
Expand All @@ -29,6 +33,15 @@ func main() {
os.Exit(0)
}

// if dotfile is set
if *dotFile != "" {
// Place dotFile/env/envName/sharpdev.yml in ./env
err := downloadDotFile(*dotFile, *envName)

check(err, "Failed to download dotfile")
os.Exit(0)
}

// Load sharpdev file
devFile := loadFile(parent)
if devFile.Version == 0 {
Expand Down Expand Up @@ -58,8 +71,6 @@ func main() {
if err != nil {
fmt.Println(err)
}

return
}

// Deals with client Errors
Expand All @@ -71,7 +82,7 @@ func check(e error, msg string) {
if os.Getenv("DEV") == "TRUE" {
fmt.Println(e)
}
log.Fatal(msg)
log.Fatal(msg + "\n" + e.Error())
}
}

Expand Down Expand Up @@ -106,7 +117,6 @@ Here are all the scripts you have available:
}

func runScript(name string, devFile config) error {

// Check if version is correct
err := checkVersion(devFile)
check(err, "Incorrect version. \nCurrently running 1.0, Script is running "+fmt.Sprint(devFile.Version))
Expand Down Expand Up @@ -136,7 +146,6 @@ func runScript(name string, devFile config) error {
}

func runCommand(commStr string, devFile config) error {

if devFile.Setup != "" && !*skipSetup {
// add setup command to commStr
commStr = devFile.Setup + "\n" + commStr
Expand All @@ -150,9 +159,6 @@ func runCommand(commStr string, devFile config) error {
commStr = strings.ReplaceAll(commStr, key, val)
}

// Replace "\n" with &&
strings.Replace(commStr, "\n", "&&", -1)

// Run command through OS args
cmd := exec.Command("/bin/sh", "-c", commStr)

Expand All @@ -166,7 +172,6 @@ func runCommand(commStr string, devFile config) error {
}

func placeInputArgs(commStr string) string {

if len(flag.Args()) == 0 {
return commStr
}
Expand Down Expand Up @@ -196,7 +201,6 @@ func placeInputArgs(commStr string) string {
}

func checkVersion(devFile config) error {

if devFile.Version != 1.0 {
return errors.New("")
}
Expand Down
2 changes: 1 addition & 1 deletion src/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ type config struct {
}

// Version Number
var Version float32 = 1.7
var Version float32 = 1.8

0 comments on commit ded38f3

Please sign in to comment.