Skip to content

Commit

Permalink
[CLI] make mac cli
Browse files Browse the repository at this point in the history
make mac cli
  • Loading branch information
Nick-0314 committed Aug 29, 2023
1 parent 01487a5 commit 597700f
Show file tree
Hide file tree
Showing 9 changed files with 1,979 additions and 23 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/cli-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ jobs:
cd cli
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 make -e BINARY_SUFFIX=.linux-amd64 -e BRANCH=${{ github.ref_name }}
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 make -e BINARY_SUFFIX=.linux-arm64 -e BRANCH=${{ github.ref_name }}
# CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 make -e BINARY_SUFFIX=.darwin-amd64
# CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 make -e BINARY_SUFFIX=.darwin-arm64
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 make -e BINARY_SUFFIX=.darwin-amd64 -e BRANCH=${{ github.ref_name }}
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 make -e BINARY_SUFFIX=.darwin-arm64 -e BRANCH=${{ github.ref_name }}
cd bin/
sha256sum * > deepflow-agent.sha256sum.txt
Expand All @@ -83,14 +83,16 @@ jobs:
run: |
ossutil cp -rf cli/bin/deepflow-ctl.linux-amd64 oss://deepflow-ce/bin/ctl/${{ env.IMAGE_TAG_PREFIX }}/linux/amd64/deepflow-ctl
ossutil cp -rf cli/bin/deepflow-ctl.linux-arm64 oss://deepflow-ce/bin/ctl/${{ env.IMAGE_TAG_PREFIX }}/linux/arm64/deepflow-ctl
# ossutil cp -rf cli/bin/deepflow-ctl.darwin-amd64 oss://deepflow-ce/bin/ctl/${{ env.IMAGE_TAG_PREFIX }}/osx/amd64/deepflow-ctl
# ossutil cp -rf cli/bin/deepflow-ctl.darwin-arm64 oss://deepflow-ce/bin/ctl/${{ env.IMAGE_TAG_PREFIX }}/osx/arm64/deepflow-ctl
ossutil cp -rf cli/bin/deepflow-ctl.darwin-amd64 oss://deepflow-ce/bin/ctl/${{ env.IMAGE_TAG_PREFIX }}/darwin/amd64/deepflow-ctl
ossutil cp -rf cli/bin/deepflow-ctl.darwin-arm64 oss://deepflow-ce/bin/ctl/${{ env.IMAGE_TAG_PREFIX }}/darwin/arm64/deepflow-ctl
- name: upload cli stable artifacts
if: "startsWith(github.ref, 'refs/tags/')"
run: |
ossutil cp -rf cli/bin/deepflow-ctl.linux-amd64 oss://deepflow-ce/bin/ctl/stable/linux/amd64/deepflow-ctl
ossutil cp -rf cli/bin/deepflow-ctl.linux-arm64 oss://deepflow-ce/bin/ctl/stable/linux/arm64/deepflow-ctl
ossutil cp -rf cli/bin/deepflow-ctl.darwin-amd64 oss://deepflow-ce/bin/ctl/stable/darwin/amd64/deepflow-ctl
ossutil cp -rf cli/bin/deepflow-ctl.darwin-arm64 oss://deepflow-ce/bin/ctl/stable/darwin/arm64/deepflow-ctl
# - name: Prepare for upload package
# shell: bash
Expand Down
267 changes: 267 additions & 0 deletions cli/ctl/common/utils_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
/*
* Copyright (c) 2023 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package common

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
_ "net"
"net/http"
"os"
"regexp"
"strings"
"time"

simplejson "github.com/bitly/go-simplejson"
"github.com/spf13/cobra"
_ "github.com/vishvananda/netlink"
)

// Filter query string parameters
type Filter map[string]interface{}

// 功能:调用其他模块API并获取返回结果
func CURLPerform(method string, url string, body map[string]interface{}, strBody string) (*simplejson.Json, error) {
var err error
var contentType string
req := &http.Request{}
if strBody != "" {
reader := strings.NewReader(strBody)
req, err = http.NewRequest(method, url, reader)
contentType = "application/x-www-form-urlencoded"
} else {
bodyStr, _ := json.Marshal(&body)
reader := bytes.NewReader(bodyStr)
req, err = http.NewRequest(method, url, reader)
contentType = "application/json"
}

if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Accept", "application/json, text/plain")
req.Header.Set("X-User-Id", "1")
req.Header.Set("X-User-Type", "1")

return parseResponse(req)
}

func parseResponse(req *http.Request) (*simplejson.Json, error) {
errResponse, _ := simplejson.NewJson([]byte("{}"))
// TODO: 通过配置文件获取API超时时间
client := &http.Client{Timeout: time.Second * 30}
resp, err := client.Do(req)
if err != nil {
return errResponse, errors.New(fmt.Sprintf("curl (%s) failed, (%v)", req.URL, err))
}

defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errResponse, errors.New(fmt.Sprintf("read (%s) body failed, (%v)", req.URL, err))
}
if resp.StatusCode != http.StatusOK {
return errResponse, errors.New(fmt.Sprintf("curl (%s) failed, (%v)", req.URL, string(respBytes)))
}

response, err := simplejson.NewJson(respBytes)
if err != nil {
return errResponse, errors.New(fmt.Sprintf("parse (%s) body failed, (%v)", req.URL, err))
}

optStatus := response.Get("OPT_STATUS").MustString()
if optStatus != "" && optStatus != SUCCESS {
description := response.Get("DESCRIPTION").MustString()
return response, errors.New(fmt.Sprintf("curl (%s) failed, (%v)", req.URL, description))
}
return response, nil
}

func CURLPostFormData(url, contentType string, body *bytes.Buffer) (*simplejson.Json, error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Accept", "application/json, text/plain")
req.Header.Set("X-User-Id", "1")
req.Header.Set("X-User-Type", "1")
req.Close = true

return parseResponse(req)
}

func CURLResponseRawJson(method string, url string) (*simplejson.Json, error) {
errResponse, _ := simplejson.NewJson([]byte("{}"))

// TODO: 通过配置文件获取API超时时间
client := &http.Client{Timeout: time.Second * 30}

var err error
req := &http.Request{}
req, err = http.NewRequest(method, url, nil)

if err != nil {
return errResponse, err
}
req.Header.Set("Accept", "application/json, text/plain")
req.Header.Set("X-User-Id", "1")
req.Header.Set("X-User-Type", "1")

resp, err := client.Do(req)
if err != nil {
return errResponse, errors.New(fmt.Sprintf("curl (%s) failed, (%v)", url, err))
}

defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errResponse, errors.New(fmt.Sprintf("read (%s) body failed, (%v)", url, err))
}

response, err := simplejson.NewJson(respBytes)
if resp.StatusCode != http.StatusOK {
var description string
_, ok := response.CheckGet("DESCRIPTION")
if ok {
description = response.Get("DESCRIPTION").MustString()
}
return response, errors.New(fmt.Sprintf("curl (%s) failed, (%v %v)", url, resp.StatusCode, description))
}

return response, nil
}

func GetDefaultRouteIP() string {
defaultRouteIP := "127.0.0.1"
return defaultRouteIP
}

type Server struct {
IP string
Port uint32
RpcPort uint32
SvcPort uint32
}

func GetServerInfo(cmd *cobra.Command) *Server {
ip, _ := cmd.Flags().GetString("ip")
port, _ := cmd.Flags().GetUint32("api-port")
rpcPort, _ := cmd.Flags().GetUint32("rpc-port")
svcPort, _ := cmd.Flags().GetUint32("svc-port")
return &Server{ip, port, rpcPort, svcPort}
}

func PrettyPrint(data interface{}) {
val, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
fmt.Println(string(val))
}

func JsonFormat(jsonByte []byte) (string, error) {
var str bytes.Buffer
err := json.Indent(&str, jsonByte, "", " ")
if err != nil {
return "", err
}
return str.String(), nil
}

// GetByFilter 通过 Get 方法获取数据,自动拼接 url param 参数
func GetByFilter(url string, body, filters map[string]interface{}) (*simplejson.Json, error) {
if !strings.HasSuffix(url, "/") {
url = url + "/"
}
if !strings.HasSuffix(url, "?") {
url = url + "?"
}

i, count := 0, len(filters)
for k, v := range filters {
url += fmt.Sprintf("%v=%v", k, v)
if i < count-1 {
url += "&"
}
i++
}
return CURLPerform("GET", url, body, "")
}

var chinesePunctuationRegex = regexp.MustCompile("[(\u4e00-\u9fa5)(\u3002|\uff1f|\uff01|\uff0c|\u3001|\uff1b|\uff1a|\u201c|\u201d|\u2018|\u2019|\uff08|\uff09|\u300a|\u300b|\u3010|\u3011|\u007e)]+")

func IsChineseChar(str string) bool {
for _, r := range str {
if chinesePunctuationRegex.MatchString(string(r)) {
return true
}
}
return false
}

func ConvertControllerAddrToPodIP(controllerIP string, controllerPort uint32) (string, error) {
url := fmt.Sprintf("http://%s:%d/v1/controllers/", controllerIP, controllerPort)
resp, err := CURLResponseRawJson("GET", url)
if err != nil {
return "", err
}
var podIP string
for c := range resp.Get("DATA").MustArray() {
controller := resp.Get("DATA").GetIndex(c)
pIP := controller.Get("POD_IP").MustString()
if controllerIP == pIP || controller.Get("IP").MustString() == controllerIP {
podIP = pIP
break
}
}
if podIP == "" {
return "", errors.New(fmt.Sprintf("request (%s) get pod ip failed", url))
}
return podIP, nil
}

func GetURLInfo(cmd *cobra.Command, urlPath string) {

server := GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d", server.IP, server.Port) + urlPath

response, err := CURLPerform("GET", url, nil, "")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}

responseByte, err := response.MarshalJSON()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}

var str bytes.Buffer
err = json.Indent(&str, responseByte, "", " ")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}

fmt.Println(str.String())
}
File renamed without changes.
8 changes: 5 additions & 3 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.18

require (
github.com/bitly/go-simplejson v0.5.0
github.com/deepflowio/deepflow/message v0.0.0-20230821033337-4663bbf77a09
github.com/deepflowio/deepflow/server v0.0.0-20230815065845-7ac50b3813a0
github.com/deepflowio/deepflow/message v0.0.0-20230824032327-76b439a46f5f
github.com/deepflowio/deepflow/server v0.0.0-20230828094550-61c74948e82a
github.com/golang/protobuf v1.5.2
github.com/mattn/go-runewidth v0.0.14
github.com/olekukonko/tablewriter v0.0.5
Expand All @@ -16,6 +16,8 @@ require (
sigs.k8s.io/yaml v1.3.0
)

replace cloud.google.com/go => cloud.google.com/go v0.103.0

require (
github.com/ClickHouse/clickhouse-go/v2 v2.1.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
Expand Down Expand Up @@ -83,7 +85,7 @@ require (
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230209215440-0dfe4f8abfcc // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/alexcesaro/statsd.v2 v2.0.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 597700f

Please sign in to comment.