Skip to content

Add alias import feature #6475

@isquicha

Description

@isquicha

gh alias import

The problem

After creating many gh aliases, change to another computer is hard because I have to add every alias manually. 😢
As we already have gh alias list, the output of this command should be the input of a gh alias import command. 😄

Proposed solution

At least one of those forms should be valid:

  1. gh alias import -f aliases_file
  2. cat aliases_file | gh alias import
  3. cat aliases_file | xargs gh alias set

I tried something like 3 but the format is not compatible.
I ended up creating a Python script that opens a file with gh alias list output and sets the new aliases:

~/gh_aliases.py

import subprocess
from os import getenv
from pathlib import Path

ALIAS_INDEX = 0
EXPANSION_INDEX = 1

file_content = Path(f"{getenv('HOME')}/gh_aliases").read_text().split("\n")

aliases_and_expansions = [
    full_alias.replace("\t", "").strip().split(":")
    for full_alias in file_content
    if full_alias != ""
]

for alias in aliases_and_expansions:
    subprocess.run(["gh", "alias", "set", alias[ALIAS_INDEX], alias[EXPANSION_INDEX]])

So the execution is:

  • In Computer 1
    • gh alias list > gh_aliases
    • copy gh_aliases file to Computer 2 (maybe with gh gist create)
  • In Computer 2
    • paste gh_aliases in the user's home directory
    • create gh_aliases.py with the content above in the user's home directory*
    • run gh alias set --shell 'ai' 'python ~/gh_aliases.py'*
    • run gh ai

Steps marked with "*" are needed only once. All other are needed every time the aliases in Computer 1 changes.

Instead of ai I tried to create the alias import alias, but I received the following error: could not create alias: "alias import" is already a gh command. Is this command already in development? I tried to use, but received unknown command "import" for "gh alias". gh version 2.18.0 (2022-10-18)

Something similar to my workaround should be delivered with gh.

Another solution

Another possible solution is tho make the output of gh alias list compatible with gh alias list | xargs gh alias set, so we can simple copy the output to a file and run the import with xargs. The problem here is that this would make the current gh alias list command incompatible with the new versions, unless it uses a custom flag (e.g. gh alias list --export-for-import | xargs gh alias set).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementa request to improve CLIhelp wantedContributions welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions