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
18 changes: 13 additions & 5 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import sys

TARGET_INSTALL_DIR = "~/.prepare-code"
# TARGET_INSTALL_DIR = "/tmp/prepare-code"

expressInstall = False
ConfigFileName = "config.yaml"
Expand All @@ -28,6 +30,8 @@ def clone_and_build():
os.system("rm -rf /tmp/prepare-code")

tmp_cmd = "cd /tmp && git clone https://github.com/OBrutus/prepare-code.git"
# tmp_cmd = "cd /tmp && cp -r /Users/obrutus/kode/prepare-code /tmp/"

status_code = os.system(tmp_cmd)

if status_code != 0:
Expand All @@ -49,19 +53,21 @@ def add_to_path():
if platform == "darwin":
shell_config = os.path.expanduser("~/.zshrc")
with open(shell_config, "a") as f:
f.write('\nexport PATH="$HOME/.prepare-code:$PATH"\n')
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.")


def setup_nix():
if not remove_dir_if_exists("~/.prepare-code", "Do you want to remove it "
if not remove_dir_if_exists(TARGET_INSTALL_DIR, "Do you want to remove it "
"and continue? (Y/n)"):
return None

if not clone_and_build():
return None
os.system("mkdir -p ~/.prepare-code")
os.system("mv /tmp/prepare-code/* ~/.prepare-code")

os.system("mkdir -p "+TARGET_INSTALL_DIR)
os.system("mv /tmp/prepare-code/* " + TARGET_INSTALL_DIR)
print("Setup completed to clone.")
if expressInstall:
choice = 'y'
Expand All @@ -70,7 +76,7 @@ def setup_nix():
choice = input(choice_prompt).strip().lower() or 'y'
if choice == 'y':
add_to_path()
return os.path.expanduser("~/.prepare-code")
return os.path.expanduser(TARGET_INSTALL_DIR)


def setup():
Expand All @@ -91,6 +97,8 @@ def setup():
def create_config_file(install_dir: str):
content = "install_dir: " + install_dir + "\n"
content += "bypass_prompt: " + "false" + "\n"
content += "preferred_editor: nvim \n"
content += "open_in_editor: false \n"

file = open(os.path.join(install_dir, ConfigFileName), 'w')
file.write(content)
Expand Down
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"prepare-code/src/config"
"prepare-code/src/editor"
"prepare-code/src/platform"
"prepare-code/src/session"
)
Expand Down Expand Up @@ -31,7 +32,7 @@ func main() {
// which platform it is
platform, err := platform.GetPlatform(url)
if err != nil {
fmt.Println("Error while getting platform: ", err)
fmt.Println("[-] Error while getting platform: ", err)
return
}

Expand All @@ -41,17 +42,22 @@ func main() {
language := getLanguage()
session, err := session.NewSession(platform, language)
if err != nil {
fmt.Println("Error while creating session: ", err)
fmt.Println("[-] Error while creating session: ", err)
return
}

err = session.CreateFile()
if err != nil {
fmt.Println("Error while creating file: ", err)
fmt.Println("[-] Error while creating file: ", err)
return
}

fmt.Println("File created successfully!")
fmt.Println("[+] File created successfully!")

err = editor.TryOpenInEditor(session.FileName)
if err != nil {
fmt.Println("[-] Err: ", err)
}
}

func getLanguage() string {
Expand Down
22 changes: 15 additions & 7 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ var yamlFileName = "config.yaml"
var config *Config

type Config struct {
InstallDir string `yaml:"install_dir"`
BypassPrompt bool `yaml:"bypass_prompt"`
installDir string `yaml:"install_dir"`
bypassPrompt bool `yaml:"bypass_prompt"`
prefferedEditor string `yaml:"preferred_editor"`
openInEditor bool `yaml:"open_in_editor"`
}

func getOldInstallDirPath() string {
Expand All @@ -29,8 +31,10 @@ func setConfig() error {
configValue := GetConfigMap()

config = &Config{
InstallDir: getOldInstallDirPath(),
BypassPrompt: types.GetBoolFromString(configValue["bypass_prompt"].(string)),
installDir: getOldInstallDirPath(),
bypassPrompt: types.GetBoolFromString(configValue["bypass_prompt"].(string)),
prefferedEditor: configValue["preferred_editor"].(string),
openInEditor: types.GetBoolFromString(configValue["open_in_editor"].(string)),
}

return nil
Expand Down Expand Up @@ -64,7 +68,7 @@ func GetConfigMap() map[string]interface{} {

func GetInstallDir() string {
if config != nil {
return config.InstallDir
return config.installDir
}

err := setConfig()
Expand All @@ -75,13 +79,17 @@ func GetInstallDir() string {
return getOldInstallDirPath()
}

return config.InstallDir
return config.installDir
}

func CanBypassPrompt() bool {
if config == nil {
setConfig()
}

return config.BypassPrompt
return config.bypassPrompt
}

func GetEditor() (string, bool) {
return config.prefferedEditor, false
}
1 change: 1 addition & 0 deletions src/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
install_dir: /Users/obrutus/.prepare-code
bypass_prompt: true
preferred_editor: nvim
open_in_editor: false
4 changes: 2 additions & 2 deletions src/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func isCiEnv() bool {
func Test_WithExistingConfig(t *testing.T) {
// Reset global config state before test
config = &Config{
InstallDir: "/custom/install/path",
BypassPrompt: true,
installDir: "/custom/install/path",
bypassPrompt: true,
}

result := GetInstallDir()
Expand Down
28 changes: 28 additions & 0 deletions src/editor/local_editor_open.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package editor

import (
"errors"
"fmt"
"os"
"os/exec"
"prepare-code/src/config"
)

var (
ErrOpenNotPreffered = errors.New("User preffered not to open in editor")
)

func TryOpenInEditor(fileName string) error {
editor, toOpen := config.GetEditor()
if !toOpen {
return ErrOpenNotPreffered
}

fmt.Println("Opening the file:", fileName, "in", editor)
cmd := exec.Command(editor, fileName)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

return cmd.Run()
}
Loading