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