rechange interface for huawei smn
This commit is contained in:
parent
abddfc91c3
commit
bb46f5adc4
6 changed files with 2 additions and 2 deletions
101
scripts/hwsmn/main.go
Normal file
101
scripts/hwsmn/main.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
msg string
|
||||
cfgFile string
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "huawei",
|
||||
Short: "huawei smn notify",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// 如果配置文件存在,则读取配置文件
|
||||
if cfgFile != "" {
|
||||
viper.SetConfigFile(cfgFile)
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
fmt.Println("Error reading config file:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
feishu := viper.GetString("feishu")
|
||||
|
||||
if err := postRequest(feishu, map[string]interface{}{
|
||||
"msg_type": "text",
|
||||
"content": map[string]string{
|
||||
"text": msg,
|
||||
},
|
||||
}); err != nil {
|
||||
fmt.Println("Error sending message:", err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func main() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd.Flags().StringVarP(&msg, "msg", "m", "", "data for feishu webhook")
|
||||
|
||||
rootCmd.MarkFlagRequired("msg")
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
bin, err := os.Executable()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
binPath := filepath.Dir(bin)
|
||||
viper.AddConfigPath(".")
|
||||
viper.AddConfigPath(binPath)
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigName("config")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // 从环境变量中读取配置
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
|
||||
func postRequest(url string, data map[string]interface{}) error {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("HTTP status code: %d", resp.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue