github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/blog/_posts/2022-10-06-dynamic-cli-commands.md (about)

     1  ---
     2  layout: post
     3  title:  "Dynamic CLI commands"
     4  author: Asim Aslam
     5  date:   2022-10-06 10:00:00
     6  ---
     7  One of the things I love the most about Micro is dynamic CLI commands. This is a feature that showed up in version 3.0. 
     8  Micro started out with a fixed set of commands like most command line tools. The API however knows how to map HTTP requests
     9  to service dynamically. We wondered, what if you could do the same for the CLI. It turns out, you can. Here's an example.
    10  
    11  <center>
    12  <img src="{{ site.baseurl }}/blog/images/dynamic-cli.png" style="width:100%; max-width: 600px; height=auto" />
    13  </center>
    14  <br>
    15  
    16  To understand how this works we need to first understand a little bit about the internals of Micro. Every service is 
    17  registered in a central registry including it's name, endpoints and request/response format. This enables us to inspect 
    18  the registry for the details we need.
    19  
    20  Calling `micro get service weather` we can see what this looks like.
    21  
    22  ```
    23  service  weather
    24  
    25  Endpoint: Weather.Now
    26  
    27  Request: {
    28          location string
    29  }
    30  
    31  Response: {
    32          location string
    33          region string
    34          country string
    35          latitude float64
    36          longitude float64
    37          timezone string
    38          local_time string
    39          temp_c float64
    40          temp_f float64
    41          feels_like_c float64
    42          feels_like_f float64
    43          humidity int32
    44          cloud int32
    45          daytime bool
    46          condition string
    47          icon_url string
    48          wind_mph float64
    49          wind_kph float64
    50          wind_direction string
    51          wind_degree int32
    52  }
    53  ```
    54  
    55  As you can see, the registered data includes an endpoint called `Weather.Now` and the Request/Response which tells us what 
    56  arguments we need to pass in and what comes back. Using this we can construct a CLI command parser to dynamically map a 
    57  request.
    58  
    59  In our case the CLI command format is `micro [service] [endpoint] [args]` e.g `micro weather now --location=london`.
    60  
    61  We can actually map any command like this. For example, let's say we want to do something basic like get Marc Andreessen's tweets.
    62  
    63  <center>
    64  <img src="{{ site.baseurl }}/blog/images/twitter.png" style="width:100%; max-width: 600px; height=auto" />
    65  </center>
    66  <br>
    67  
    68  I can't emphasize enough how powerful this has been since it's introduction. The ability to programmatically test and use 
    69  services and APIs from the CLI in this way enables all sorts of scripting functionality but it has also just been a far more 
    70  intuitive way to use services from the command line.
    71  
    72  One of the lagging features which I'd love to address in future is unix style piping of responses from one service as requests 
    73  into the next. The unix pipe provides composition like no other, hopefuly at some point we'll be able to replicate this 
    74  as part of the Micro CLI.
    75  
    76  Try out [Micro](https://github.com/tickoalcantara12/micro) for yourself!
    77  
    78  Update: Oh forgot to mention, the command line also generates help output
    79  
    80  <center>
    81  <img src="{{ site.baseurl }}/blog/images/help1.png" style="width:100%; max-width: 600px; height=auto" />
    82  </center>
    83  <br>
    84  
    85  <center>
    86  <img src="{{ site.baseurl }}/blog/images/help2.png" style="width:100%; max-width: 600px; height=auto" />
    87  </center>
    88  <br>
    89