Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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'
Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
}
Expand Down
3 changes: 2 additions & 1 deletion src/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 37 additions & 0 deletions src/system/command.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions src/session/os_user.go → src/system/os_user.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package session
package system

import (
"log"
"os"
"os/user"
)

func getUserName() string {
func GetUserName() string {
curUser, err := user.Current()
if err != nil || curUser == nil {
log.Println("Error getting current user:", err)
Expand Down
65 changes: 65 additions & 0 deletions src/system/update.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading