6565 skipTLSVerify bool
6666 proxy string
6767
68+ force bool
69+ initHost string
70+
6871 rootCmd = & cobra.Command {
6972 Use : "privatebin" ,
7073 Version : fmt .Sprintf ("%s-%s (%s)" , version , commit , date ),
@@ -334,6 +337,63 @@ var (
334337 return nil
335338 },
336339 }
340+
341+ initCmd = & cobra.Command {
342+ Use : "init" ,
343+ Short : "Generate a configuration file" ,
344+ SilenceUsage : true ,
345+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
346+ if cfgPath == "" {
347+ homeDir , err := os .UserHomeDir ()
348+ if err != nil {
349+ return fmt .Errorf ("cannot get user home directory: %w" , err )
350+ }
351+
352+ cfgPath = path .Join (homeDir , ".config" , "privatebin" , "config.json" )
353+ }
354+
355+ return nil
356+ },
357+ RunE : func (cmd * cobra.Command , args []string ) error {
358+ if ! force {
359+ if _ , err := os .Stat (cfgPath ); err == nil {
360+ return fmt .Errorf ("configuration file already exists at %s, use --force to overwrite" , cfgPath )
361+ }
362+ }
363+
364+ cfg := map [string ]any {
365+ "bin" : []map [string ]any {
366+ {
367+ "name" : "" ,
368+ "host" : initHost ,
369+ },
370+ },
371+ "expire" : "1day" ,
372+ "formatter" : "plaintext" ,
373+ "gzip" : true ,
374+ }
375+
376+ data , err := json .MarshalIndent (cfg , "" , " " )
377+ if err != nil {
378+ return fmt .Errorf ("cannot marshal configuration: %w" , err )
379+ }
380+
381+ data = append (data , '\n' )
382+
383+ dir := filepath .Dir (cfgPath )
384+ if err := os .MkdirAll (dir , 0o755 ); err != nil {
385+ return fmt .Errorf ("cannot create configuration directory: %w" , err )
386+ }
387+
388+ if err := os .WriteFile (cfgPath , data , 0o600 ); err != nil {
389+ return fmt .Errorf ("cannot write configuration file: %w" , err )
390+ }
391+
392+ fmt .Fprintf (os .Stdout , "%s\n " , cfgPath )
393+
394+ return nil
395+ },
396+ }
337397)
338398
339399func init () {
@@ -358,7 +418,10 @@ func init() {
358418 showCmd .Flags ().StringVar (& password , "password" , "" , "the paste password" )
359419 showCmd .Flags ().BoolVar (& skipTLSVerify , "skip-tls-verify" , false , "skip TLS certificate verification" )
360420
361- rootCmd .AddCommand (showCmd , createCmd )
421+ initCmd .Flags ().BoolVar (& force , "force" , false , "overwrite existing configuration file" )
422+ initCmd .Flags ().StringVar (& initHost , "host" , "https://privatebin.net" , "the host of the default privatebin instance" )
423+
424+ rootCmd .AddCommand (showCmd , createCmd , initCmd )
362425}
363426
364427func main () {
0 commit comments