peter-bread / gamon

GitHub Account Manager
MIT License
3 stars 0 forks source link

Flexible context #55

Open peter-bread opened 7 months ago

peter-bread commented 7 months ago

*Maybe

Instead of needing to follow a strict file structure, let users create a file that sets the account for that directory and all its children, unless one of its children also has the file.

This will mean users won't need to have a root directory that contains all their repos if they don't want.

For example, it would enable something like this:

/home/john/
|-- personal/
|   |-- memes/
|   |-- finances/
|   |-- repos/
|       |-- .gh_account
|-- school/
|   |-- maths/
|   |-- computer-science/
|       |-- reports/
|       |-- repos/
|           |-- .gh_account

The two repos/ directories do not share the same parent, but they each contain a .gh_account file which can be read by gamon to set the correct account.

For users (like me) who like having all their repos together, it is easy to achieve the same result as using GAM_REPO_ROOT_DIR:

/home/john/
|-- repos/
|   |-- personal/
|   |   |-- .gh_account
|   |-- work/
|       |-- .gh_account
|-- memes/
|-- finances/

I might create a new command (e.g. gam account [path] that will use the promptui module that will list the available accounts (from ~/.config/gh/hosts.yml) and let you select one, which should make it a bit harder for users to just create .gh_account and put whatever they want in it.

peter-bread commented 7 months ago

This will not actually be breaking. Not for now at least.

I will make it so it checks for a config file first, and if it doesn't find one, then attempt to use the original method.

peter-bread commented 7 months ago

Might be something along the lines of this:

# Function to find GitHub account from configuration file (.github_account)
find_account_from_config_file() {
    local directory="$PWD"
    local config_file=".gh_account"
    local account

    while [ "$directory" != "/" ]; do
        if [ -f "$directory/$config_file" ]; then
            account=$(< "$directory/$config_file")
            echo "$account"
            return 0
        fi
        directory=$(dirname "$directory")
    done

    return 1
}

# Main function to select the GitHub account
select_github_account() {
    local account_from_config

    # Check if there's a configuration file specifying the account
    if account_from_config=$(find_account_from_config_file); then
        echo "Using account specified in .gh_account: $account_from_config"
        if ! gh auth switch --user "$account_from_config"; then
            echo "Error: Could not switch to account $account_from_config" >&2
            return 1
        fi
        return 0
    fi

    # Proceed with the existing logic if no configuration file found
    gh_auth_switch_on_pwd
}

# Example usage: cd into a directory and select GitHub account
cd_and_select_account() {
    builtin cd "$@" || return 1
    select_github_account
}

# get current shell
current_shell=$(ps -p $$ -ocomm=)

# calls function to check and/or switch github account on every cd
if [[ $current_shell == *"zsh"* ]]; then
    autoload -U add-zsh-hook
    add-zsh-hook chpwd cd_and_select_account
elif [[ $current_shell == *"bash"* ]]; then
    cd() {
        cd_and_select_account "$@" || return 1
    }
else
    echo "Error: Unsupported shell. Only zsh and bash are supported." >&2
    return 1
fi
peter-bread commented 7 months ago

Realised as well I can add code in here to load specific .gitconfig files based on the account in .gh_account.

peter-bread commented 7 months ago
# Function to find GitHub account from configuration file (.gh_account)
find_account_from_config_file() {
    local directory="$PWD"
    local config_file=".gh_account"
    local account

    while [ "$directory" != "/" ]; do
        if [ -f "$directory/$config_file" ]; then
            account=$(< "$directory/$config_file")
            echo "$account"
            return 0
        fi
        directory=$(dirname "$directory")
    done

    return 1
}

# Function to load gitconfig based on the GitHub account
load_gitconfig() {
    local account="$1"
    local gitconfig_file="$HOME/.gitconfig_$account"

    if [ -f "$gitconfig_file" ]; then
        echo "Loading gitconfig for account: $account"
        export GIT_CONFIG="$gitconfig_file"
    else
        echo "No specific gitconfig found for account: $account"
    fi
}

# Main function to select the GitHub account and load gitconfig
select_github_account() {
    local account_from_config

    # Check if there's a configuration file specifying the account
    if account_from_config=$(find_account_from_config_file); then
        echo "Using account specified in .gh_account: $account_from_config"
        if ! gh auth switch --user "$account_from_config"; then
            echo "Error: Could not switch to account $account_from_config" >&2
            return 1
        fi
        load_gitconfig "$account_from_config"
        return 0
    fi

    # Proceed with the existing logic if no configuration file found
    gh_auth_switch_on_pwd
}

# Example usage: cd into a directory and select GitHub account
cd_and_select_account() {
    builtin cd "$@" || return 1
    select_github_account
}

# get current shell
current_shell=$(ps -p $$ -ocomm=)

# calls function to check and/or switch github account on every cd
if [[ $current_shell == *"zsh"* ]]; then
    autoload -U add-zsh-hook
    add-zsh-hook chpwd cd_and_select_account
elif [[ $current_shell == *"bash"* ]]; then
    cd() {
        cd_and_select_account "$@" || return 1
    }
else
    echo "Error: Unsupported shell. Only zsh and bash are supported." >&2
    return 1
fi
peter-bread commented 1 week ago

Simpler solution:

Use something like direnv or mise, and in that file set an environment variable like GH_ACCOUNT.

Then just check against that variable