1

I want, in one command with args to config kubeconfig, that is able to connect to k8s cluster.

I tried the following which does not work.

cfg:
    mkdir ~/.kube

kube: cfg
    touch config $(ARGS)

In the args the user should pass the config file content of the cluster (kubeconfig).

If there is a shorter way please let me know.

update

I've used the following which (from the answer) is partially solve the issue.

kube: cfg
    case "$(ARGS)" in \
      ("") printf "Please provide ARGS=/some/path"; exit 1;; \
      (*)  cp "$(ARGS)" /some/where/else;; \
    esac

The problem is because of the cfg which is creating the dir in case the user not providing the args and in the second run when providing the path the dir is already exist and you get an error, is there a way to avoid it ? something like if the arg is not provided dont run the cfg

NSS
  • 755
  • 1
  • 11
  • 30
  • What would be an example of the content of $(ARGS)? – Jens May 01 '20 at 14:36
  • @Jens - lets say it's a path to a file, https://github.com/zecke/Kubernetes/blob/master/docs/user-guide/kubeconfig-file.md#components-of-a-kubeconfig-file – NSS May 01 '20 at 14:54
  • I don't understand anything. `touch` only modifies time stamps (or creates an empty file). It does not make sense to "touch files with the contents of another file". Do you want to create a file with some contents? Then maybe printf or cp is a better approach. – Jens May 01 '20 at 15:57
  • Ok so with `cp` how would you do it when getting user input for file location? – NSS May 01 '20 at 16:55
  • Are you asking how to write Make variable values to a file? Or how Make can receive input from the user? – Beta May 01 '20 at 18:21

1 Answers1

2

I assume the user input is the pathname of a file. The make utility can take variable assignments as arguments, in the form of make NAME=VALUE. You refer to these in your Makefile as usual, with $(NAME). So something like

kube: cfg
    case "$(ARGS)" in \
      ("") printf "Please provide ARGS=/some/path"; exit 1;; \
      (*)  cp "$(ARGS)" /some/where/else;; \
    esac

called with

make ARGS=/some/path/file kube

would then execute cp /some/path/file /some/where/else. If that is not what you were asking, please rephrase the question, providing exact details of what you want to do.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • Thank you very much this is what I need , is there a way to validate that the user is providing the args, the problem is that if the user by mistake doesn't provide the `args` and just call `make kube` the previous target is called with the `mkdir kube` and the folder is created , now if he provide the args and run the command as expected, he will get an error as the folder is already created – NSS May 01 '20 at 21:05
  • @NinaS Yes, you can do that with a bit of shell scripting. I prefer case/in/esac over test. See my modified answer. – Jens May 02 '20 at 06:20
  • Thank you very much! I tried it and I got the following error: `user: projects $ make kube mkdir ~/.kube mkdir: cannot create directory '/home/user/.kube': File exists make: *** [Makefile:2: cfg] Error 1` the problem i think is because of the `cfg` which is creating the dir in case the user not providing the `args` and in the second run when providing the path it already exist and you get an error, is there a way to avoid it ? something like if the `arg` is not provided dont run the `cfg` . thank you! – NSS May 02 '20 at 07:47
  • You can use `mkdir -p ~/.kube` which will not complain if a directory already exists. – Jens May 04 '20 at 10:33