github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/tests/functests/p0152.yml (about)

     1  doc_meta: |
     2    folder: user-interaction
     3    title: chained pipein from stdin
     4    head: |
     5      You can use UPcmd to chain the command output as input and choose a task to handle the data processing
     6  
     7    sections:
     8      - title: Demo
     9        content: |
    10          ```
    11            ▶ which uptestx
    12            uptestx () {
    13              up ngo task -d ./tests/functests -t c0152.yml --configdir=./tests/functests
    14            }
    15  
    16            ▶ curl -X GET "http://httpbin.org/get" -H "accept: application/json" |uptestx c0152
    17              % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
    18              Dload  Upload   Total   Spent    Left  Speed
    19              100   268  100   268    0     0    456      0 --:--:-- --:--:-- --:--:--   455
    20            loading [Config]:  ./tests/functests/upconfig.yml
    21            Main config:
    22              Version -> 1.0.0
    23              RefDir -> ./tests/functests
    24              WorkDir -> cwd
    25              AbsWorkDir -> /up-project/up
    26              TaskFile -> c0152
    27              Verbose -> v
    28              ModuleName -> self
    29              ShellType -> /bin/sh
    30              MaxCallLayers -> 8
    31              MaxModuelCallLayers -> 256
    32            work dir: /up-project/up
    33            -exec task: task
    34            loading [Task]:  ./tests/functests/c0152
    35            module: [self] instance id: [dev]
    36            pipein: {
    37              "args": {},
    38              "headers": {
    39                "Accept": "application/json",
    40                "Host": "httpbin.org",
    41                "User-Agent": "curl/7.54.0",
    42                "X-Amzn-Trace-Id": "Root=1-5f0475af-708d0cca558974455b9e2fe8"
    43              },
    44              "origin": "14.202.210.195",
    45              "url": "http://httpbin.org/get"
    46            }
    47  
    48            Task1: [task ==> task: process the stdin pipe-in data ]
    49            -Step1:
    50            ~SubStep1: [inspect:  ]
    51              1: inspect[exec_vars](*core.Cache)({
    52                "up_runtime_task_pipe_in_content": "{\n  \"args\": {}, \n  \"headers\": {\n    \"Accept\": \"application/json\", \n    \"Host\": \"httpbin.org\", \n    \"User-Agent\": \"curl/7.54.0\", \n    \"X-Amzn-Trace-Id\": \"Root=1-5f0475af-708d0cca558974455b9e2fe8\"\n  }, \n  \"origin\": \"14.202.210.195\", \n  \"url\": \"http://httpbin.org/get\"\n}\n"
    53            })
    54  
    55            ~SubStep2: [print: this is the pipe in string content ]
    56              {
    57                "args": {},
    58                "headers": {
    59                  "Accept": "application/json",
    60                  "Host": "httpbin.org",
    61                  "User-Agent": "curl/7.54.0",
    62                  "X-Amzn-Trace-Id": "Root=1-5f0475af-708d0cca558974455b9e2fe8"
    63                },
    64                "origin": "14.202.210.195",
    65                "url": "http://httpbin.org/get"
    66              }
    67  
    68            ~SubStep3: [toObj:  ]
    69            ~SubStep4: [printObj: this is the pipe in object ]
    70            object:
    71              my_http_get_response: {
    72                "args": {
    73                },
    74                "headers": {
    75                  "Accept": "application/json",
    76                  "Host": "httpbin.org",
    77                  "User-Agent": "curl/7.54.0",
    78                  "X-Amzn-Trace-Id": "Root=1-5f0475af-708d0cca558974455b9e2fe8"
    79                },
    80                "origin": "14.202.210.195",
    81                "url": "http://httpbin.org/get"
    82              }
    83  
    84            ~SubStep5: [print: access the object ]
    85            "headers": {
    86              User-Agent: curl/7.54.0,
    87              "X-Amzn-Trace-Id": "Root=1-5f0475af-708d0cca558974455b9e2fe8",
    88              "Accept": "application/json",
    89              "Host": "httpbin.org",
    90            }
    91  
    92          ```
    93  
    94      - title: how does it work
    95        content: |
    96          This case shows that the output of a restapi call to http://httpbin.org/get will be the a pipe in input to UPcmd, then the result is automatically saved to register name: up_runtime_task_pipe_in_content
    97  
    98          Initially the input result is always a string, then we use toObj cmd to auto convert it to a object named my_http_get_response, then we will be able to access the whole object
    99  
   100          Please note that the command will use the assigned task name for execution, however it is up to the user to design the workflow to decide how and what to use the register pipein value
   101  
   102      - title: extended use case
   103        content: |
   104          You could use UPcmd in following way:
   105  
   106          * register the input and parse it to an object, process it and pass it on for your task
   107          * chain it through to another UPcmd, eg.
   108          ```
   109            cmd1 | up ngo task1 | up ngo task2 | cmd2 | up ngo task3 | ......
   110          ```
   111  
   112          It is not recommend to chain directly to another UPcmd as you could route it internally.
   113  
   114  notes:
   115    - add std pipe in feature
   116    - register the piped in content to a default register name
   117    - leave it to be up to the task to take care how to process the data
   118  
   119  tasks:
   120    -
   121      name: task
   122      desc: process the stdin pipe-in data
   123      task:
   124  
   125        -
   126          func: cmd
   127          do:
   128            - name: inspect
   129              cmd:
   130                - exec_vars
   131  
   132            - name: print
   133              desc: this is the pipe in string content
   134              cmd: '{{.up_runtime_task_pipe_in_content}}'
   135  
   136            - name: toObj
   137              cmd:
   138                reg: my_http_get_response
   139                fromkey: up_runtime_task_pipe_in_content
   140  
   141            - name: printObj
   142              desc: this is the pipe in object
   143              cmd: my_http_get_response
   144  
   145            - name: print
   146              desc: access the object
   147              cmd: |
   148                "headers": {
   149                  User-Agent: {{index .my_http_get_response.headers "User-Agent"}},
   150                  "X-Amzn-Trace-Id": "{{index .my_http_get_response.headers "X-Amzn-Trace-Id"}}",
   151                  "Accept": "{{.my_http_get_response.headers.Accept}}",
   152                  "Host": "{{.my_http_get_response.headers.Host}}",
   153                }