Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What technologies are used to implement the CLI of Kubernetes

2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces which technologies are used to achieve Kubernetes's CLI, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Cobra introduction

Cobra is a library for creating powerful modern CLI applications and a program for generating application and command files.

Cobra is used for many Go projects, such as Kubernetes, Hugo, and Github CLI. This list contains a broader list of projects that use Cobra.

Overview

Cobra is a library that provides a simple interface to create a powerful modern CLI interface, similar to git&go tools.

Cobra is also an application that will generate your application framework for rapid development of Cobra-based applications.

Cobra provides:

Easy to use son-based CLI:app server,app fetch and so on.

Fully compatible with POSIX logo (including short version and long version)

Nested subcommand

Global, local and cascade flags

Easily generated applications and commands and cobra init appname&cobra add cmdname

Wise advice (app srver). Do you mean app server?

Help for automatically generating commands and flags

Automatic help logo recognition-HMAG Mutual help and so on.

Automatic completion of shells automatically generated for your application (bash,zsh,fish,powershell)

Automatically generate man pages for your application

Command aliases so that you can change the content without breaking them

Define your own help, usage, etc. Flexibility.

Optional tight integration with viper for 12-factor applications

Concept

Cobra is based on the structure of commands, parameters, and flags.

Commands represent actions, Args is things, and Flags is the modifier of these actions.

The best applications read like sentences when used, and as a result, users intuitively know how to interact with them.

The pattern followed is APPNAME VERB NOUN-ADJECTIVE. Or APPNAME COMMAND ARG-- FLAG

Some real examples can better illustrate this point.

In the following example, "Server" is the command and "Port" is the flag:

Hugo server-port=1313

In this command, we tell Git to clone the contents of the url.

Git clone URL-bare

Installation

It's easy to use a cobra. First, use go get to install the latest version of the library. This command installs the cobra generator executable as well as the library and its dependencies:

Go get-u github.com/spf13/cobra

Next, include Cobra in your application:

Import "github.com/spf13/cobra"

Introduction

You are welcome to provide your own organization, but usually Cobra-based applications will follow the following organizational structure:

▾ appName/ ▾ cmd/ add.go your.go commands.go here.go main.go

In Cobra applications, main.go files are usually very naked. It has a purpose: to initialize the cobra.

Package main import ("{pathToYourApp} / cmd") func main () {cmd.Execute ()}

Cobra usage

Directory structure

Image-20210218100009073

Base command

Create a root.go file and define a base command

Package cmd import ("github.com/spf13/cobra") var RootCmd = & cobra.Command {Use: "gonne", Run: func (cmd * cobra.Command, args [] string) {if len (args) = = 0 {cmd.Help () return}},}

Using commands in the main function is as simple as that

Main.go file

Package main import ("demo-practice/cobra/cmd"fmt"os") func main () {if err: = cmd.RootCmd.Execute (); err! = nil {fmt.Println (err) os.Exit (1)}}

Compile in windows environment

Go build-o gonne.exe

Execute the command to see the effect, and the output is as follows

Gonne.exe

Usage: gonne [flags] gonne [command]

Available Commands: help Help about any command version Print the version number of Gonne

Flags:-h,-- help help for gonne

Use "gonne [command]-help" for more information about a command.

Subcommand

It is also quite easy to add subcommands to the base command. There is no need to write any code in the base command and main method. You only need to create a new go file, and multiple subcommands are independent of each other. How elegant the code is, farewell to all kinds of case.

Add a version of the command usage:

Add a version.go file to the cmd directory as follows

Package cmd import "github.com/spf13/cobra" func init () {RootCmd.AddCommand (versionCmd)} var versionCmd = & cobra.Command {Use: "version", Short: "Print the version number of Gonne", Run: func (cmd * cobra.Command, args [] string) {println ("gonne version is 0.0.1")},}

The init () function adds this command to the base command

Execute gonne version after compilation

Gonne version is 0.0.1 Thank you for reading this article carefully. I hope the article "what technologies are used to achieve Kubernetes CLI" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report