| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | |
| 7 | "github.com/dennwc/btrfs" |
| 8 | "github.com/spf13/cobra" |
| 9 | ) |
| 10 | |
| 11 | func init() { |
| 12 | RootCmd.AddCommand( |
| 13 | SubvolumeCmd, |
| 14 | SendCmd, |
| 15 | ReceiveCmd, |
| 16 | ) |
| 17 | |
| 18 | SubvolumeCmd.AddCommand( |
| 19 | SubvolumeCreateCmd, |
| 20 | SubvolumeDeleteCmd, |
| 21 | SubvolumeListCmd, |
| 22 | ) |
| 23 | |
| 24 | SendCmd.Flags().StringP("parent", "p", "", "Send an incremental stream from <parent> to <subvol>.") |
| 25 | } |
| 26 | |
| 27 | var RootCmd = &cobra.Command{ |
| 28 | Use: "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]", |
| 29 | Short: "Use --help as an argument for information on a specific group or command.", |
| 30 | } |
| 31 | |
| 32 | var SubvolumeCmd = &cobra.Command{ |
| 33 | Use: "subvolume <command> <args>", |
| 34 | Aliases: []string{"subvol", "sub", "sv"}, |
| 35 | } |
| 36 | |
| 37 | var SubvolumeCreateCmd = &cobra.Command{ |
| 38 | Use: "create [-i <qgroupid>] [<dest>/]<name>", |
| 39 | Short: "Create a subvolume", |
| 40 | Long: `Create a subvolume <name> in <dest>. If <dest> is not given subvolume <name> will be created in the current directory.`, |
| 41 | RunE: func(cmd *cobra.Command, args []string) error { |
| 42 | if len(args) == 0 { |
| 43 | return fmt.Errorf("subvolume not specified") |
| 44 | } else if len(args) > 1 { |
| 45 | return fmt.Errorf("only one subvolume name is allowed") |
| 46 | } |
| 47 | return btrfs.CreateSubVolume(args[0]) |
| 48 | }, |
| 49 | } |
| 50 | |
| 51 | var SubvolumeDeleteCmd = &cobra.Command{ |
| 52 | Use: "delete [options] <subvolume> [<subvolume>...]", |
| 53 | Short: "Delete subvolume(s)", |
| 54 | Long: `Delete subvolumes from the filesystem. The corresponding directory |
| 55 | is removed instantly but the data blocks are removed later. |
| 56 | The deletion does not involve full commit by default due to |
| 57 | performance reasons (as a consequence, the subvolume may appear again |
| 58 | after a crash). Use one of the --commit options to wait until the |
| 59 | operation is safely stored on the media.`, |
| 60 | RunE: func(cmd *cobra.Command, args []string) error { |
| 61 | for _, arg := range args { |
| 62 | if err := btrfs.DeleteSubVolume(arg); err != nil { |
| 63 | return err |
| 64 | } |
| 65 | } |
| 66 | return nil |
| 67 | }, |
| 68 | } |
| 69 | |
| 70 | var SubvolumeListCmd = &cobra.Command{ |
| 71 | Use: "list <mount>", |
| 72 | Short: "List subvolumes", |
| 73 | Aliases: []string{"ls"}, |
| 74 | RunE: func(cmd *cobra.Command, args []string) error { |
| 75 | if len(args) != 1 { |
| 76 | return fmt.Errorf("expected one destination argument") |
| 77 | } |
| 78 | fs, err := btrfs.Open(args[0], true) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | defer fs.Close() |
| 83 | list, err := fs.ListSubvolumes(nil) |
| 84 | if err == nil { |
| 85 | for _, v := range list { |
| 86 | fmt.Printf("%+v\n", v) |
| 87 | } |
| 88 | } |
| 89 | return err |
| 90 | }, |
| 91 | } |
| 92 | |
| 93 | var SendCmd = &cobra.Command{ |
| 94 | Use: "send [-v] [-p <parent>] [-c <clone-src>] [-f <outfile>] <subvol> [<subvol>...]", |
| 95 | Short: "Send the subvolume(s) to stdout.", |
| 96 | Long: `Sends the subvolume(s) specified by <subvol> to stdout. |
| 97 | <subvol> should be read-only here.`, |
| 98 | RunE: func(cmd *cobra.Command, args []string) error { |
| 99 | parent, _ := cmd.Flags().GetString("parent") |
| 100 | return btrfs.Send(os.Stdout, parent, args...) |
| 101 | }, |
| 102 | } |
| 103 | |
| 104 | var ReceiveCmd = &cobra.Command{ |
| 105 | Use: "receive [-v] [-f <infile>] [--max-errors <N>] <mount>", |
| 106 | Short: "Receive subvolumes from stdin.", |
| 107 | Long: `Receives one or more subvolumes that were previously |
| 108 | sent with btrfs send. The received subvolumes are stored |
| 109 | into <mount>.`, |
| 110 | RunE: func(cmd *cobra.Command, args []string) error { |
| 111 | if len(args) != 1 { |
| 112 | return fmt.Errorf("expected one destination argument") |
| 113 | } |
| 114 | return btrfs.Receive(os.Stdin, args[0]) |
| 115 | }, |
| 116 | } |
| 117 | |
| 118 | func main() { |
| 119 | if err := RootCmd.Execute(); err != nil { |
| 120 | fmt.Println(err) |
| 121 | os.Exit(-1) |
| 122 | } |
| 123 | } |