package main import ( "flag" "fmt" "github.com/oylshe1314/framework/errors" "github.com/oylshe1314/framework/server" "github.com/oylshe1314/framework/ssh" "github.com/oylshe1314/framework/util" "os" ) type SshConfig struct { Address string `json:"address"` User string `json:"user"` Pass string `json:"pass"` KeyFile string `json:"keyFile"` } type PublishConfig struct { Platforms []string `json:"platforms"` LocalPath string `json:"localPath"` RemotePath string `json:"remotePath"` } type PublishClient struct { sshConfig *SshConfig publishConfig *PublishConfig sshClient *ssh.Client } func (this *PublishClient) WithSshConfig(sshConfig *SshConfig) { this.sshConfig = sshConfig } func (this *PublishClient) WithPublishConfig(publishConfig *PublishConfig) { this.publishConfig = publishConfig } func (this *PublishClient) Init() (err error) { if this.sshConfig == nil { return errors.Error("'sshConfig' can not be nil") } if this.sshConfig.Address == "" { return errors.Error("'sshConfig.Address' can not be empty") } if this.sshConfig.User == "" { return errors.Error("'sshConfig.User' can not be empty") } if this.publishConfig == nil { return errors.Error("'publishConfig' can not be nil") } if len(this.publishConfig.Platforms) == 0 { return errors.Error("'publishConfig.Platforms' can not be empty") } if this.publishConfig.LocalPath == "" { return errors.Error("'publishConfig.LocalPath' can not be empty") } if this.publishConfig.RemotePath == "" { return errors.Error("'publishConfig.RemotePath' can not be empty") } if this.publishConfig.LocalPath[len(this.publishConfig.LocalPath)-1] != '/' { this.publishConfig.LocalPath += "/" } if this.publishConfig.RemotePath[len(this.publishConfig.RemotePath)-1] != '/' { this.publishConfig.RemotePath += "/" } return nil } func (this *PublishClient) Close() (err error) { if this.sshClient != nil { return this.sshClient.Close() } return nil } func (this *PublishClient) connect() (err error) { var key []byte if this.sshConfig.KeyFile != "" { key, err = os.ReadFile(this.sshConfig.KeyFile) if err != nil { return err } this.sshConfig.Pass = "" } this.sshClient, err = ssh.Open(this.sshConfig.Address, this.sshConfig.User, this.sshConfig.Pass, key) if err != nil { return err } return nil } func (this *PublishClient) publish(platform string) (err error) { if this.sshClient == nil { return errors.Error("please connect the server first") } var platforms []string if platform == "all" { platforms = this.publishConfig.Platforms } else { platforms = []string{platform} } var pkgDir = "latest_packet" for _, pp := range platforms { var md = util.TimeFormat("0102") _, err = this.sshClient.Exec("cd", this.publishConfig.RemotePath, "&&", "mv", pkgDir, fmt.Sprintf("%s.%s", pkgDir, md), "&&", "mkdir", pkgDir) if err != nil { return err } var pkgFile = fmt.Sprintf("%s.zip", pp) err = this.sshClient.Upload(this.publishConfig.LocalPath+pkgDir+"/"+pkgFile, this.publishConfig.RemotePath+pkgDir+"/") if err != nil { return err } _, err = this.sshClient.Exec("cd", this.publishConfig.RemotePath+pkgDir, "&&", "unzip", pkgFile, "&&", "rm", "-f", pkgFile) if err != nil { return err } } return nil } func main() { var platform string flag.StringVar(&platform, "p", "all", "Specify the platform to publish") flag.Parse() if platform == "" { platform = "all" } opt, err := server.ReadOptions("./config.json") if err != nil { fmt.Println(err) return } var client = &PublishClient{} fmt.Println("init publisher client") err = opt.Init(client) if err != nil { fmt.Println(err) return } fmt.Println("connect ssh server") err = client.connect() if err != nil { fmt.Println(err) return } defer client.Close() fmt.Printf("publish %s package\n", platform) err = client.publish(platform) if err != nil { fmt.Println(err) return } fmt.Println("publish finished") }