| 75 | } |
| 76 | |
| 77 | func (o *pluginPackageOptions) run(out io.Writer) error { |
| 78 | // Check if the plugin path exists and is a directory |
| 79 | fi, err := os.Stat(o.pluginPath) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | if !fi.IsDir() { |
| 84 | return errors.New("plugin package only supports directories, not tarballs") |
| 85 | } |
| 86 | |
| 87 | // Load and validate plugin metadata |
| 88 | pluginMeta, err := plugin.LoadDir(o.pluginPath) |
| 89 | if err != nil { |
| 90 | return fmt.Errorf("invalid plugin directory: %w", err) |
| 91 | } |
| 92 | |
| 93 | // Create destination directory if needed |
| 94 | if err := os.MkdirAll(o.destination, 0755); err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | // If signing is requested, prepare the signer first |
| 99 | var signer *provenance.Signatory |
| 100 | if o.sign { |
| 101 | // Load the signing key |
| 102 | signer, err = provenance.NewFromKeyring(o.keyring, o.key) |
| 103 | if err != nil { |
| 104 | return fmt.Errorf("error reading from keyring: %w", err) |
| 105 | } |
| 106 | |
| 107 | // Get passphrase |
| 108 | passphraseFetcher := o.promptUser |
| 109 | if o.passphraseFile != "" { |
| 110 | passphraseFetcher, err = o.passphraseFileFetcher() |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Decrypt the key |
| 117 | if err := signer.DecryptKey(passphraseFetcher); err != nil { |
| 118 | return err |
| 119 | } |
| 120 | } else { |
| 121 | // User explicitly disabled signing |
| 122 | fmt.Fprint(out, "WARNING: Skipping plugin signing. This is not recommended for plugins intended for distribution.\n") |
| 123 | } |
| 124 | |
| 125 | // Now create the tarball (only after signing prerequisites are met) |
| 126 | // Use plugin metadata for filename: PLUGIN_NAME-SEMVER.tgz |
| 127 | metadata := pluginMeta.Metadata() |
| 128 | filename := fmt.Sprintf("%s-%s.tgz", metadata.Name, metadata.Version) |
| 129 | tarballPath := filepath.Join(o.destination, filename) |
| 130 | |
| 131 | tarFile, err := os.Create(tarballPath) |
| 132 | if err != nil { |
| 133 | return fmt.Errorf("failed to create tarball: %w", err) |
| 134 | } |