kube-aliases icon indicating copy to clipboard operation
kube-aliases copied to clipboard

Generate aliases

Open Dbz opened this issue 2 years ago • 25 comments

Generate aliases!

Dbz avatar Aug 20 '21 00:08 Dbz

I'd like to modify the bin/ stuff before we merge. Some of it could be reduced to aliases. The others might work better using go and then some aliases. But if you wanna merge sooner that's fine by me. 

patmessina avatar Aug 20 '21 01:08 patmessina

Also. We should tag the current version before moving on. Just in case people wanna keep using previous version.

patmessina avatar Aug 20 '21 01:08 patmessina

I'm in no rush to merge. I'd love to get this polished before that. I'll actually work off a branch and then merge into this branch. I only created a PR because it's the easiest way for me to view the diff 😂 , and we can comment on the code here.

Also. We should tag the current version before moving on. Just in case people wanna keep using previous version.

Good idea 👍

Dbz avatar Aug 20 '21 02:08 Dbz

I created a PR to rebase this branch on top of master: https://github.com/Dbz/kube-aliases/pull/21

It definitely needs some 👀

Dbz avatar Aug 20 '21 03:08 Dbz

So with generation, I currently give everything all the base commands. But I think it may be better to have each resource have a list of commands that it wants. This way we whitelist the cmds we want for each resource.

Actually, I think this might simplify it.

patmessina avatar Aug 20 '21 15:08 patmessina

Hey, I merged and changed a few things.

Offhand, the only thing was an underscore in the tests seems unconventional. Also, when reading in an aliases I dont think order is guaranteed so the test fails sometimes. Im going to adjust it a little bit but need to go for the day and am commiting.

patmessina avatar Aug 23 '21 22:08 patmessina

Also, wanna move test cases yaml files into generate/tests just so we can test specific things there.

Ive never used require before. Will have to check it out.

patmessina avatar Aug 23 '21 22:08 patmessina

Earlier commit I moved types into their own thing. I believe its more conventional.

patmessina avatar Aug 23 '21 22:08 patmessina

What do you think about removing bin/generate-kube-aliases from git? Seems like something to add to the .gitignore. I didn't want to do that without asking you first

Dbz avatar Aug 24 '21 16:08 Dbz

What do you think about removing bin/generate-kube-aliases from git? Seems like something to add to the .gitignore. I didn't want to do that without asking you first

Yea, I think so. Im thinking we generate a default file for users to source and only if they want they can use the generate tool. Figure we can make them into releases. Probably should have it ignore bin completely.

patmessina avatar Aug 24 '21 18:08 patmessina

This last commit allows for additional aliases. Essentially we can generate everything to a single file.

Im happy to revert back if this isnt the direction youd like to go.

patmessina avatar Aug 24 '21 18:08 patmessina

Guess let me know what you think from here.

Couple of thoughts. I have.

  • Probably can change cmds to a list like I had additional. I dont think it needs to be a map.
  • More tests - make sure things fail nicely
  • Build a release
  • Redo readme - most users will probably just want to source the generated file

patmessina avatar Aug 24 '21 19:08 patmessina

OK, here is the PR to remove the leaky abstraction. I did a little bit of extra refactoring to combine Generate and GenerateAdditional: https://github.com/Dbz/kube-aliases/pull/26

Dbz avatar Aug 25 '21 05:08 Dbz

Well, I think this is looking great.

I like all of your bullets. I would add we should make the user provided aliases go into a separate file so we're not overriding it on update.

Dbz avatar Aug 25 '21 06:08 Dbz

Hmmm... that seems to have broken stuff. Tests are passing, but if you actually use the tool its broken. I have fixed that (need to initialize the map) but now the tests are always passing. So gotta figure out what that is all about.

It seems to be true on commit before yours so I shall see.

patmessina avatar Aug 25 '21 16:08 patmessina

It is the TestMain function. Itll be fixed next commit.

patmessina avatar Aug 25 '21 16:08 patmessina

So I realize, in order to allow folks to generate their own stuff, we do need to include a binary in git :( . I usually dislike that, so maybe let's generate one per release?

Dbz avatar Aug 26 '21 04:08 Dbz

Possible solution:

Don't provide it in the repository. And pretty much have one file that they can source. It'll have good defaults.

Have a binary that we keep with the release. That's where it belongs anyway. If users want to generate them they can download that release.

I imagine most people just wanna source a file and be done. But just in case, think we should have all the commands be copy and paste.

patmessina avatar Aug 26 '21 13:08 patmessina

I made some modifications for my personal use to create aliases for powershell. I don't know go, so I am vary of opening a PR, but the basic idea is as follows

diff --git a/generate/pkg/generate/generate.go b/generate/pkg/generate/generate.go
index 069a456..e083c42 100644
--- a/generate/pkg/generate/generate.go
+++ b/generate/pkg/generate/generate.go
@@ -104,7 +104,9 @@ func GenerateAlias(w io.Writer, aliases *types.AliasCMDs) error {
                        lastResource = alias.Resource
                }

-               fmt.Fprintf(w, "alias %v=\"%v\"\n", aliasName, aliasCommand)
+               ps1prefix := "ps1_"
+               fmt.Fprintf(w, "function %v%v {%v $args}\n", ps1prefix, aliasName, aliasCommand)
+               fmt.Fprintf(w, "New-Alias -Name %v -Value %v%v\n", aliasName, ps1prefix, aliasName)
        }

        var comment string
@@ -121,7 +123,9 @@ func GenerateAlias(w io.Writer, aliases *types.AliasCMDs) error {
                        comment = alias.Comment
                }

-               fmt.Fprintf(w, "alias %v=\"%v\"\n", alias.Short, alias.CMD)
+               ps1prefix := "ps1_"
+               fmt.Fprintf(w, "function %v%v {%v $args}\n", ps1prefix, alias.Short, alias.CMD)
+               fmt.Fprintf(w, "New-Alias -Name %v -Value %v%v\n", alias.Short, ps1prefix, alias.Short)
        }
        return nil
 }

It would be very nice if something like this could be accommodated. I will work on fixing it...

anupamsr avatar Nov 24 '21 11:11 anupamsr

Hi @anupamsr . I'm happy to accommodate :)

What should do is work together on a PR to create a new generated file specifically for powershell users. Should should be able to do that with a minimal amount of code.

Dbz avatar Nov 25 '21 21:11 Dbz

@anupamsr -- started the support. Thank ya. 6199509f06cede43c5d2d732e81a3d53b28bd271

patmessina avatar Dec 08 '21 22:12 patmessina

Hi. Is there a plan to add aliases for kubectl context? I mean, something like

--- a/aliases.yaml
+++ b/aliases.yaml
@@ -164,4 +164,13 @@ additional:
   - short: keit
     cmd: "kubectl exec -it"
     comment: "Exec alias."
+  - short: kk
+    cmd: "kubectl config"
+    comment: "Modify kubeconfig files."
+  - short: kkc
+    cmd: "kubectl config get-contexts"
+    comment: "Describe one or many contexts."
+  - short: kku
+    cmd: "kubectl config use-context"
+    comment: "Set the current-context in a kubeconfig file."

I am just about learning kubernetes devops so it if you think there is a better way, I would really appriciate knowing that.

anupamsr avatar Jan 29 '22 06:01 anupamsr

Sure, if you want you can open a pull request and Ill merge it in. If not, Ill add it.

patmessina avatar Jan 30 '22 04:01 patmessina

Yes, if you're going to open a PR which would be awesome, please base it off of this branch!

Dbz avatar Jan 30 '22 19:01 Dbz

Done. Please have a look: https://github.com/Dbz/kube-aliases/pull/34

anupamsr avatar Feb 03 '22 13:02 anupamsr