|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "bytes" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + |
| 12 | + "github.com/MakeNowJust/heredoc" |
| 13 | + "github.com/cli/go-gh/v2/pkg/api" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +type deployCmdFlags struct { |
| 18 | + dir string |
| 19 | + app string |
| 20 | + revisionName string |
| 21 | + sha string |
| 22 | +} |
| 23 | + |
| 24 | +func zipDirectory(sourceDir, destinationZip string) error { |
| 25 | + zipFile, err := os.Create(destinationZip) |
| 26 | + if err != nil { |
| 27 | + return fmt.Errorf("error creating zip file '%s': %w", destinationZip, err) |
| 28 | + } |
| 29 | + defer zipFile.Close() |
| 30 | + |
| 31 | + zipWriter := zip.NewWriter(zipFile) |
| 32 | + defer zipWriter.Close() |
| 33 | + |
| 34 | + err = filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error { |
| 35 | + if err != nil { |
| 36 | + return fmt.Errorf("error accessing path '%s': %w", path, err) |
| 37 | + } |
| 38 | + |
| 39 | + relPath, err := filepath.Rel(sourceDir, path) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("error calculating relative path for '%s': %w", path, err) |
| 42 | + } |
| 43 | + |
| 44 | + if info.IsDir() { |
| 45 | + if relPath == "." { |
| 46 | + return nil |
| 47 | + } |
| 48 | + relPath += "/" |
| 49 | + } |
| 50 | + |
| 51 | + header, err := zip.FileInfoHeader(info) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("error creating zip header for '%s': %w", path, err) |
| 54 | + } |
| 55 | + header.Name = relPath |
| 56 | + if !info.IsDir() { |
| 57 | + header.Method = zip.Deflate |
| 58 | + } |
| 59 | + |
| 60 | + writer, err := zipWriter.CreateHeader(header) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("error creating zip writer for '%s': %w", path, err) |
| 63 | + } |
| 64 | + |
| 65 | + if !info.IsDir() { |
| 66 | + file, err := os.Open(path) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("error opening file '%s': %w", path, err) |
| 69 | + } |
| 70 | + defer file.Close() |
| 71 | + |
| 72 | + _, err = io.Copy(writer, file) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("error writing file '%s' to zip: %w", path, err) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | + }) |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("error zipping directory '%s': %w", sourceDir, err) |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func init() { |
| 89 | + deployCmdFlags := deployCmdFlags{} |
| 90 | + deployCmd := &cobra.Command{ |
| 91 | + Use: "deploy", |
| 92 | + Short: "Deploy app to GitHub Runtime", |
| 93 | + Long: heredoc.Doc(` |
| 94 | + Deploys a directory to a GitHub Runtime app |
| 95 | + `), |
| 96 | + Example: heredoc.Doc(` |
| 97 | + $ gh runtime deploy --dir ./dist --app my-app [--sha <sha>] |
| 98 | + # => Deploys the contents of the 'dist' directory to the app named 'my-app'. |
| 99 | + `), |
| 100 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 101 | + if deployCmdFlags.dir == "" { |
| 102 | + return fmt.Errorf("--dir flag is required") |
| 103 | + } |
| 104 | + if deployCmdFlags.app == "" { |
| 105 | + return fmt.Errorf("--app flag is required") |
| 106 | + } |
| 107 | + |
| 108 | + if _, err := os.Stat(deployCmdFlags.dir); os.IsNotExist(err) { |
| 109 | + return fmt.Errorf("directory '%s' does not exist", deployCmdFlags.dir) |
| 110 | + } |
| 111 | + |
| 112 | + _, err := os.ReadDir(deployCmdFlags.dir) |
| 113 | + if err != nil { |
| 114 | + return fmt.Errorf("error reading directory '%s': %v", deployCmdFlags.dir, err) |
| 115 | + } |
| 116 | + |
| 117 | + // Zip the directory |
| 118 | + zipPath := fmt.Sprintf("%s.zip", deployCmdFlags.dir) |
| 119 | + err = zipDirectory(deployCmdFlags.dir, zipPath) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("error zipping directory '%s': %v", deployCmdFlags.dir, err) |
| 122 | + } |
| 123 | + defer os.Remove(zipPath) |
| 124 | + |
| 125 | + client, err := api.DefaultRESTClient() |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("error creating REST client: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + deploymentsUrl := fmt.Sprintf("runtime/%s/deployment/bundle", deployCmdFlags.app) |
| 131 | + params := url.Values{} |
| 132 | + |
| 133 | + if deployCmdFlags.revisionName != "" { |
| 134 | + params.Add("revision_name", deployCmdFlags.revisionName) |
| 135 | + } |
| 136 | + |
| 137 | + if deployCmdFlags.sha != "" { |
| 138 | + params.Add("revision", deployCmdFlags.sha) |
| 139 | + } |
| 140 | + |
| 141 | + if len(params) > 0 { |
| 142 | + deploymentsUrl += "?" + params.Encode() |
| 143 | + } |
| 144 | + |
| 145 | + fmt.Printf("Deploying app to %s\n", deploymentsUrl) |
| 146 | + |
| 147 | + // body is the full zip RAW |
| 148 | + body, err := os.ReadFile(zipPath) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("error reading zip file '%s': %v", zipPath, err) |
| 151 | + } |
| 152 | + |
| 153 | + err = client.Post(deploymentsUrl, bytes.NewReader(body), nil) |
| 154 | + if err != nil { |
| 155 | + return fmt.Errorf("error deploying app: %v", err) |
| 156 | + } |
| 157 | + |
| 158 | + fmt.Printf("Successfully deployed app\n") |
| 159 | + return nil |
| 160 | + }, |
| 161 | + } |
| 162 | + deployCmd.Flags().StringVarP(&deployCmdFlags.dir, "dir", "d", "", "The directory to deploy") |
| 163 | + deployCmd.Flags().StringVarP(&deployCmdFlags.app, "app", "a", "", "The app to deploy") |
| 164 | + deployCmd.Flags().StringVarP(&deployCmdFlags.revisionName, "revision-name", "r", "", "The revision name to deploy") |
| 165 | + deployCmd.Flags().StringVarP(&deployCmdFlags.sha, "sha", "s", "", "SHA of the app being deployed") |
| 166 | + |
| 167 | + rootCmd.AddCommand(deployCmd) |
| 168 | +} |
0 commit comments