diff --git a/install.py b/install.py index 94f9cbe..74c3ff0 100755 --- a/install.py +++ b/install.py @@ -55,7 +55,7 @@ def add_to_path(): with open(shell_config, "a") as f: f.write('\nexport PATH=' + TARGET_INSTALL_DIR + '":$PATH"\n') print(f"Added to PATH in {shell_config}. Please restart your terminal " - "or run 'source {shell_config}' to apply changes.") + f"or run 'source {shell_config}' to apply changes.") def setup_nix(): @@ -66,8 +66,7 @@ def setup_nix(): if not clone_and_build(): return None - os.system("mkdir -p "+TARGET_INSTALL_DIR) - os.system("mv /tmp/prepare-code/* " + TARGET_INSTALL_DIR) + os.system("mv /tmp/prepare-code " + TARGET_INSTALL_DIR) print("Setup completed to clone.") if expressInstall: choice = 'y' diff --git a/main.go b/main.go index ad7ac36..67e323a 100644 --- a/main.go +++ b/main.go @@ -7,13 +7,13 @@ import ( "prepare-code/src/editor" "prepare-code/src/platform" "prepare-code/src/session" + "prepare-code/src/system" ) func main() { // start of the CLI application // accept URL var url string - if len(os.Args) < 2 { fmt.Print("Enter the URL of problem: ") fmt.Scanln(&url) @@ -25,6 +25,17 @@ func main() { } else if os.Args[1] == "raw" { genericHandover() return + } else if os.Args[1] == "update" { + fmt.Println("____________________") + fmt.Println("Attempting to update") + fmt.Println("^^^^^^^^^^^^^^^^^^^^") + o, err := system.Update() + if err != nil { + fmt.Println("[-] Update Failed with error", err) + } else { + fmt.Println("[+] Update Successfull", o) + } + return } else { url = os.Args[1] } diff --git a/src/session/session.go b/src/session/session.go index 3c8a673..66f7bc1 100644 --- a/src/session/session.go +++ b/src/session/session.go @@ -8,6 +8,7 @@ import ( "path/filepath" "prepare-code/src/config" "prepare-code/src/platform/generic_platform" + "prepare-code/src/system" "prepare-code/src/types" "strings" "time" @@ -41,7 +42,7 @@ func NewSession(platform types.Platform, language string) (Session, error) { Platform: platform, Language: language, FileName: fileName, - userName: getUserName(), + userName: system.GetUserName(), startDate: time.Now(), targetDirectory: dir, submissionVersion: 0, diff --git a/src/system/command.go b/src/system/command.go new file mode 100644 index 0000000..1f9b5bb --- /dev/null +++ b/src/system/command.go @@ -0,0 +1,37 @@ +package system + +import ( + "errors" + "fmt" + "os/exec" + "runtime" +) + +// just put command we will make sure which shell to execute +func executeCommand(commands []string, targetDir string) (string, error) { + shell := "" + switch runtime.GOOS { + case "windows": + shell = "cmd" + case "linux": + shell = "bash" + case "darwin": + shell = "zsh" + } + + if shell == "" { + return "", errors.New("Unable to detect OS") + } + + cmd := exec.Command(shell, commands...) + if targetDir != "" { + cmd.Dir = targetDir + } + + out, err := cmd.CombinedOutput() + if err != nil { + return "", errors.New(fmt.Sprintf("Execution failed: %s", err)) + } + + return string(out), nil +} diff --git a/src/session/os_user.go b/src/system/os_user.go similarity index 90% rename from src/session/os_user.go rename to src/system/os_user.go index 9490884..c811f70 100644 --- a/src/session/os_user.go +++ b/src/system/os_user.go @@ -1,4 +1,4 @@ -package session +package system import ( "log" @@ -6,7 +6,7 @@ import ( "os/user" ) -func getUserName() string { +func GetUserName() string { curUser, err := user.Current() if err != nil || curUser == nil { log.Println("Error getting current user:", err) diff --git a/src/system/update.go b/src/system/update.go new file mode 100644 index 0000000..5b1095e --- /dev/null +++ b/src/system/update.go @@ -0,0 +1,65 @@ +package system + +import ( + "errors" + "io/fs" + "os" + "path/filepath" + "prepare-code/src/config" + "strings" +) + +// TODO: Add auto update logic + +var ( + ErrNonGitUpdateNotSupported = errors.New("ErrNonGitUpdateNotSupported") +) + +const DefaultMessage = "Update Not supported, kindly reinstall!" + +func Update() (string, error) { + if !isGitInitiated() { + return DefaultMessage, ErrNonGitUpdateNotSupported + } + + return updateWithGit() +} + +func isGitInitiated() bool { + dir := config.GetInstallDir() + + info, err := os.Stat(filepath.Join(dir, ".git")) + if err != nil { + // Returns true if the error is specifically because the path does not exist + if errors.Is(err, fs.ErrNotExist) { + return false + } + // Any other error (e.g., permission denied) means we can't confirm + // its existence, but treat it as false or handle it explicitly. + return false + } + + return info.IsDir() +} + +func updateWithGit() (string, error) { + var output string + var err error + + // this considers that the repo is git initated + output, err = executeCommand( + strings.Split("git pull origin mainline", " "), + config.GetInstallDir(), + ) + if err != nil { + return output, err + } + + // now to install the update + output, err = executeCommand( + strings.Split("go build .", " "), + config.GetInstallDir(), + ) + + return output, err +}