Hello, I’m Luo Zhu from @Daning

This article was first published on luozhu’s official website

This article has authorized the exclusive use of the public account of the Developer community, including but not limited to editing, marking the original rights and other rights.

Participate in this event only for the gift package, XDM don’t panic

Project introduction

Tuya-panel-cli is a development tool based on Go language for developers of doodle smart panel.

The main function is to initialize a panel project based on the Tuya-Panel-Demo warehouse, package the completed project and automatically check and update the tool background.

The project was developed based on Cobra project, a popular Go CLI framework. The requirements and design goals were basically achieved as expected, which provided a starting point for further improving the developer ecosystem and added an important part (below the surface) to form the development closed-loop.

In this project, I was responsible for the whole process of demand undertaking, technology selection (described later), scheme design, scheme landing and project delivery. Of course it’s nothing to brag about, and as you can see it’s not a complicated project. The reason WHY I want to do it again is because this project is a project that I completely control from 0 to 1 to 1.1.

Project background

Doodle Intelligence is a global AI + IOT leader. The IoTOS division of Luozhu is mainly about enabling developers to use Doodle IoTOS to independently select different chips for intelligent product development.

With the growing number of internal and external developers, maintaining two sets of development tools is no longer an appropriate trend. In order to better serve the majority of developers. We need to build a unified developer platform. Developer tools (CLI) is a great place to start.

Technology selection

Luo investigated the Node, Deno, and Go solutions before actually starting development. We didn’t develop directly with Node because of Boss’s requirement that the source code not be open, and because a lot of our developers are embedded. To make it as easy as possible for developers (without having to install a runtime) we needed a solution that packaged the CLI as binary.

Node

As we all know, the CLI for Node development needs to be installed via NPM, and developers need to install the Node environment locally. This may be familiar to front-end developers, but for non-front-end developers or non-programmers, the additional configuration environment required to run your scripts may be jarring.

Is there a binary packaging scheme for Node?

The answer is yes. Luo found ocLIF and PKG in her research, both of which can package developed programs across platforms. The difference is that PKG is simply a tool that packages programs into binary executable files, whereas OCLIF is a complete CLI framework that supports a plug-in system.

So far, OCLIf seems to be the one we want, but unfortunately it was rejected because it packaged the Node environment into the installer, causing the installer to be too large and expose the source code directly (PKG was passed directly because of its rudimentary functionality).

Deno

Bamboo is a Deno enthusiasts, Deno from entry to run | 🏆 technical essay proves that I have written the first phase of the project Hello World. Because of its syntax and API proximity to Node, this was the second option to consider.

Deno fully supports cross-platform compilation with the addition of build support for Windows in 1.7.0. Seve is a simple CLI Demo I wrote.

The reason why I didn’t choose Deno was that the ecology wasn’t perfect and I needed to build my own wheels a little bit too much. For example, no mature cross-platform compilation, Flag parsing, questionnaire plug-ins, color text, loading, progress bar plug-ins. There is no CLI framework like Commander and CAC for Node. Seve was a big hole I dug to create a Deno version of Commander, but changed my mind after I contacted caC.

Go

Bamboo is also a great fan of Go, the same speed introduction Go climbing and took the nuggets column | 🏆 technology project that I have written in the second stage of Hello World. I considered Go last because, as a front-end developer, Node and Deno give me little or no psychological pressure, whereas Go is both familiar and unfamiliar to me. I’m afraid I won’t choose Go if the first two can’t meet the requirements.

Cross-platform compilation

Go itself supports compiling commands. Here is a package script written using Go’s built-in build command:

#! /bin/bash
name="tuya-panel-cli"
# Windows
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o ${name}.exe main.go
# Linux
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${name} main.go
# MacOS
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o ${name} main.go
Copy the code

While you may find the above scripts cumbersome, Go also has third-party compiler plug-ins, such as Gox and xGo.

CLI framework

Urfave is relatively easy to use, but it’s recommended for small or single-command programs. I chose COBRA. In the process of investigating Cobra, I sorted out and translated the Chinese Cobra documents conveniently, and those who need to dig can directly visit the prostitute. Let’s look at the characteristics of Cobra:

  • Simple CLIs based on subcommands: App Server, app fetch, etc.

  • Marks for full POSIX (Portable Operating System Interface) compatibility (both short and long versions)

  • Nested subcommands

  • Global, local, and cascading flags

  • Use COBRA Init appName and COBRA add cmdName to easily generate applications and commands

  • Smart tips (App Srver… Did you mean app Server)

  • Automatically generates help for commands and flags

  • Automatically identify help identifiers such as -h and –help

  • The bash generated automatically for your application completes automatically

  • Automatically generate a MAN manual for your application

  • Command aliases so that you can change the contents without breaking them

  • Flexibility to define your own help, usage, etc.

  • Optional tight integration with viper for 12factor applications

We can see from the features that Cobra can provide us with both rich features and a way to get our solution off the ground.

Practice process

What are the problems encountered in practice and how to solve them? What results were achieved, or what enhancements were made?

After the selection of technology is the practice of landing, the construction of the project framework can be directly read the Chinese documents mentioned above, here will not repeat. Below I will follow the tuya-panel-CLI implementation of the command to describe the practice process.

Init Initializes the template

Requirements: Tuya-Panel-Demo project contains all templates, we need to implement prompt the user to select the corresponding template and initialize it to the current directory

1. Release templates: GitHub CI listens to new Git Tags being pushed to the repository, and packages all templates and releases a draft release. The administrator manually writes release notes and releases the release. This process is consistent with the versioning of Deno itself. For this part of the content can read ding, a Deno GitHub Action source analysis please check.

2, select templates: The first idea is to maintain a TXT file and get the content through HTTP, which is a bit silly, every time there is a new template or deleted template has to manually maintain; The second approach is through the crawler crawl https://github.com/tuya/tuya-panel-demo/releases/latest and remove the template list. I use Colly as crawler and PROMPtui as promptui.

3, download template: I use NET/HTTP hand a file download function, and then use archiver to decompress the compression package to the cache directory, finally use copy to copy to the current directory.

Upgrade Upgrade command

Learning excellent script management from Deno shows how to manage our programs, and implementing Deno Upgrade based on Go shows how to stand on the shoulders of giants and implement your own upgrade commands. Here is a brief overview.

1. Taking a page from Deno’s script management approach, we managed the version through github Release (the same approach with Go’s plug-in management).

2. Read the source code of the Deno upgrade command and implement it with Go.

3. On the basis of imitation, the function of background check and update is realized

4. Encapsulate the Go-Release plug-in and open source it.

Package to pack

This command is migrated from an internal project. One thing to learn is to add // +build Windows at the top of the file for Windows platform compilation and // +build Linux Darwin for Linux and MAC compilation.

homebrew

In order to further facilitate Homebrew users, luzhu released tuya-panel-cli to youngjuning/homebrew-tap, please refer to the specific content of my article at that time, right? No way! Who doesn’t give out Homebrew packs?

Conclusion thinking

  • Technology selection needs to actually write the demo, what looks right may not be right
  • Technology selection should take into account time and ecology, and professional programmers should not be afraid of the world outside their comfort zone
  • The project was able to deliver on time because of the power and ease of use of the COBRA framework, and because of the prior Go fundamentals. But this is nothing to boast about, the syntax used is simple enough.
  • After the project was released, the documents were supplemented and the tips were optimized. No problems with use have been found.
  • It has not been linked with Viper, and can be added later if required.
  • In the future, systematic learning of Go language is required to better optimize the project.

The most important insight is that one should record his study and work in time, just like keeping an account.

This article is participating in the “Nuggets 2021 Spring Recruiting activity”, click to see the details of the activity