Skip to content

Commit

Permalink
✨ add ProfileConfig struct, add helper functions to handle config and…
Browse files Browse the repository at this point in the history
… git
  • Loading branch information
Shieldine committed Jul 26, 2024
1 parent 6e6be59 commit 20d274f
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
120 changes: 120 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
@@ -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
}
90 changes: 90 additions & 0 deletions internal/git.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions models/profile_config.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit 20d274f

Please sign in to comment.