diff --git a/internal/config.go b/internal/config.go new file mode 100644 index 0000000..8b29ba0 --- /dev/null +++ b/internal/config.go @@ -0,0 +1,120 @@ +package internal + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/BurntSushi/toml" + "github.com/Shieldine/git-profile/models" +) + +type Config struct { + Profiles []models.ProfileConfig `toml:"users"` +} + +var ( + Conf Config + configPath string +) + +func init() { + exePath, err := os.Executable() + if err != nil { + fmt.Println("Error determining executable path:", err) + os.Exit(1) + } + configPath = filepath.Join(filepath.Dir(exePath), "config.toml") + + if _, err := os.Stat(configPath); os.IsNotExist(err) { + file, err := os.Create(configPath) + if err != nil { + fmt.Printf("Failed to create config file: %v\n", err) + os.Exit(1) + } + file.Close() + Conf = Config{Profiles: []models.ProfileConfig{}} + } + + err = LoadConfig() + if err != nil { + fmt.Println("Error loading config file:", err) + os.Exit(1) + } +} + +func LoadConfig() error { + if _, err := toml.DecodeFile(configPath, &Conf); err != nil { + return fmt.Errorf("failed to decode internal file: %v", err) + } + return nil +} + +func SaveConfig() error { + file, err := os.Create(configPath) + if err != nil { + return fmt.Errorf("failed to save config file: %v", err) + } + defer file.Close() + if err := toml.NewEncoder(file).Encode(Conf); err != nil { + return fmt.Errorf("failed to encode config to file: %v", err) + } + return nil +} + +func AddProfile(profile models.ProfileConfig) error { + for _, existingProfile := range Conf.Profiles { + if existingProfile.ProfileName == profile.ProfileName { + return fmt.Errorf("profile with name %s already exists", profile.ProfileName) + } + } + + Conf.Profiles = append(Conf.Profiles, profile) + return SaveConfig() +} + +func EditProfile(profileName string, updatedProfile models.ProfileConfig) error { + for i, existingProfile := range Conf.Profiles { + if existingProfile.ProfileName == profileName { + Conf.Profiles[i] = updatedProfile + return SaveConfig() + } + } + return fmt.Errorf("profile with name %s not found", profileName) +} + +func DeleteProfile(profileName string) error { + for i, existingProfile := range Conf.Profiles { + if existingProfile.ProfileName == profileName { + Conf.Profiles = append(Conf.Profiles[:i], Conf.Profiles[i+1:]...) + return SaveConfig() + } + } + return fmt.Errorf("profile with name %s not found", profileName) +} + +func GetProfileByName(profileName string) models.ProfileConfig { + for _, existingProfile := range Conf.Profiles { + if existingProfile.ProfileName == profileName { + return existingProfile + } + } + return models.ProfileConfig{} +} + +func GetAllProfiles() []models.ProfileConfig { + return Conf.Profiles +} + +func GetConfigPath() string { + return configPath +} + +func ClearConfig() error { + file, err := os.Create(configPath) + if err != nil { + return fmt.Errorf("failed to reset config file: %v", err) + } + defer file.Close() + return nil +} diff --git a/internal/git.go b/internal/git.go new file mode 100644 index 0000000..5034d20 --- /dev/null +++ b/internal/git.go @@ -0,0 +1,90 @@ +package internal + +import ( + "errors" + "os" + "os/exec" + "strings" +) + +func CheckGitRepo() bool { + cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree") + if err := cmd.Run(); err != nil { + return false + } + return true +} + +func GetRepoOrigin() (string, error) { + if !CheckGitRepo() { + return "", errors.New("not a git repository") + } + cmd := exec.Command("git", "config", "--get", "remote.origin.url") + output, err := cmd.Output() + if err != nil { + return "", err + } + + originURL := strings.TrimSpace(string(output)) + + if strings.HasPrefix(originURL, "https://") { + originURL = strings.TrimPrefix(originURL, "https://") + } else if strings.HasPrefix(originURL, "http://") { + originURL = strings.TrimPrefix(originURL, "http://") + } else if strings.HasPrefix(originURL, "git@") { + originURL = strings.TrimPrefix(originURL, "git@") + if idx := strings.Index(originURL, ":"); idx != -1 { + originURL = originURL[:idx] + } + } + + if idx := strings.Index(originURL, "/"); idx != -1 { + originURL = originURL[:idx] + } + + return originURL, nil +} + +func SetUserName(name string) error { + if !CheckGitRepo() { + return errors.New("not a git repository") + } + cmd := exec.Command("git", "config", "user.name", name) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} + +func GetUserName() (string, error) { + if !CheckGitRepo() { + return "", errors.New("not a git repository") + } + cmd := exec.Command("git", "config", "--get", "user.name") + output, err := cmd.Output() + if err != nil { + return "", err + } + return strings.TrimSpace(string(output)), nil +} + +func SetUserEmail(email string) error { + if !CheckGitRepo() { + return errors.New("not a git repository") + } + cmd := exec.Command("git", "config", "user.email", email) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} + +func GetUserEmail() (string, error) { + if !CheckGitRepo() { + return "", errors.New("not a git repository") + } + cmd := exec.Command("git", "config", "--get", "user.email") + output, err := cmd.Output() + if err != nil { + return "", err + } + return strings.TrimSpace(string(output)), nil +} diff --git a/models/profile_config.go b/models/profile_config.go new file mode 100644 index 0000000..691191b --- /dev/null +++ b/models/profile_config.go @@ -0,0 +1,9 @@ +package models + +type ProfileConfig struct { + ProfileName string `toml:"profile_name"` + Name string `toml:"name"` + Email string `toml:"email"` + SigningKey string `toml:"signing_key"` + Origin string `toml:"origin"` +}