Skip to content

Latest commit

 

History

History
48 lines (36 loc) · 3.43 KB

File metadata and controls

48 lines (36 loc) · 3.43 KB

Introduction to Cobra

Cobra is a powerful library for creating modern command-line applications in Go. It's used in many popular projects like Kubernetes, Hugo, and Docker. Cobra provides:

  • Command Structure: Easily define commands and subcommands, allowing for a hierarchical and organized CLI.
  • Flag Parsing: Robust handling of command-line flags (options) and arguments.
  • Automatic Help Generation: Cobra automatically generates help messages for your commands, simplifying documentation.
  • Suggestions: Provides intelligent suggestions for mistyped commands.
  • Modular Design: Encourages a clean separation of concerns by placing commands in their own files.

Challenges in Building Network Automation CLIs with Go/Cobra

  1. Network Device Interaction: Connecting to and interacting with various network devices (routers, switches, firewalls) often requires different protocols (SSH, Telnet, NETCONF, RESTConf, gRPC) and libraries.
  2. Credential Management: Securely handling sensitive credentials for network devices (passwords, API tokens).
  3. Error Handling: Robustly managing network-related errors (connection timeouts, authentication failures, command execution errors).
  4. Idempotency: Ensuring that network operations can be run multiple times without unintended side effects.
  5. State Management: Keeping track of network device states if the CLI needs to perform sequential operations.
  6. Concurrency: Potentially managing multiple concurrent connections to devices for faster operations.
  7. Output Formatting: Presenting network data in a human-readable and machine-parseable format (tables, JSON, YAML).
  8. Configuration Management: Loading and managing application configuration (e.g., list of devices, default credentials).

Simple Example Use Case: Network Device Pinger

We will build a simple CLI that can:

  • Ping a single network device to check reachability.
  • Ping multiple network devices from a list.
  • Optionally specify a custom timeout for pings.

Overview

Day Description State
Day 1 Getting Started with Go and Cobra works
Day 2 Adding a ping Command works
Day 3 Adding Flags and Persistent Flags works
Day 4 Handling Multiple Targets with a Subcommand works
Day 5 Error Handling and Exit Codes works
Day 6 Advanced Network Interaction (Placeholder for a Real-World Scenario) works
Day 7 Configuration Management with Viper works
Day 8 Building and Best Practices works

For more informations go to: Corba CLI and the User Guide

Final ToDo

Post about your journey, what you learned on different platforms like LinkedIn, Twitter or any other of your favourite platforms. Follow up on your journey and share it with others! Use the Hashtags #NetAutoBootcamp #NetworkAutomation You can also tag us on LinkedIn with @netauto-group-rheinmain