Home > database >  GNU make read variable from a file and present to choose on prompt
GNU make read variable from a file and present to choose on prompt

Time:07-24

I do have a config file that contains multiple lines of code like

locals {
  env = {
    ns-marcus = {
      name                    = "marcus"
      vpc_name                = "vpc-marcus"
      subnet1_name            = "Subnet1-Marcus"
      subnet2_name            = "Subnet2-Marcus"
      route_table1_name             = "route-table-marcus"
      igw_name                      = "igw-marcus"
    }

    ns-phil = {
      name                    = "phil"
      vpc_name                = "vpc-phil"
      subnet1_name            = "Subnet1-phil"
      subnet2_name            = "Subnet2-phil"
      route_table1_name             = "route-table-phil"
      igw_name                      = "igw-phil"
    }

this list continuous to grow

I configured a Makefile that statically asks for the individual user on prompt and after text input commands are executed

all:
@echo "WORKSPACES AVAILABLE:"
@echo "marcus"
@echo "phil"

apply: all
@read -p "Enter Workspace Name: " workspace; \
terraform workspace select $$workspace 

the issue I am now having is that if I statically add another user3 in my config file user.tf I have to manually edit the Makefile to include another @echo line for user3

Ideally I would love to read all potential users and then prompt for the configured users in user.tf

CodePudding user response:

You can just use something like awk to extract the workspace names from your config file:

all:
    @echo "WORKSPACES AVAILABLE"
    @awk '/^user/ {print $1}' config.file

apply: all
    @read -p "Enter Workspace Name: " workspace; \
    terraform workspace select $$workspace 

CodePudding user response:

MYVAR := $(shell cat workspaces.tf | awk '{for (i=1;i<=NF;i  ) if ($$i == "name") {printf "%s\\n\n",$$3}}' workspaces.tf)
all:
    @echo "WORKSPACES AVAILABLE"
    @echo $(MYVAR)

this did it!

  • Related