Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Flexible context #55

Open
Open
@peter-bread

Description

*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.

Activity

self-assigned this
on Feb 16, 2024
peter-bread

peter-bread commented on Feb 27, 2024

@peter-bread
OwnerAuthor

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

peter-bread commented on Feb 27, 2024

@peter-bread
OwnerAuthor

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

peter-bread commented on Feb 27, 2024

@peter-bread
OwnerAuthor

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

peter-bread

peter-bread commented on Feb 27, 2024

@peter-bread
OwnerAuthor
# 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

peter-bread commented on Sep 28, 2024

@peter-bread
OwnerAuthor

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

pinned this issue on Oct 28, 2024
peter-bread

peter-bread commented on Oct 28, 2024

@peter-bread
OwnerAuthor

Need to do this bc its annoying me

peter-bread

peter-bread commented on Jan 19, 2025

@peter-bread
OwnerAuthor

Also an option:

Create a main config file which has a default account to use, then a list of paths and which account to use when in them or their children. Can still have the option of overriding with a local file:

# ~/.config/gamon/config.yaml

# default account to use
default: john-smith

# mapping of directories to the account they should use
others:
  ~/Work: jsmith
  $HOME/Uni: js123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

enhancementNew feature or requestpriorityThis should be implemented before other things

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Flexible context · Issue #55 · peter-bread/gamon