github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/cmd/git-dolt/git-dolt.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"log"
    19  	"os"
    20  	"os/exec"
    21  	"path/filepath"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"github.com/dolthub/dolt/go/cmd/git-dolt/commands"
    26  )
    27  
    28  func main() {
    29  	if _, err := exec.LookPath("dolt"); err != nil {
    30  		log.Fatal("It looks like Dolt is not installed on your system. Make sure that the `dolt` binary is in your PATH before attempting to run git-dolt commands.")
    31  	}
    32  
    33  	if filepath.Base(os.Args[0]) == "git-dolt" {
    34  		os.Args = append([]string{"git", "dolt"}, os.Args[1:]...)
    35  	}
    36  
    37  	fakeGitCmd := &cobra.Command{
    38  		Use: "git",
    39  	}
    40  
    41  	rootCmd := &cobra.Command{
    42  		Use:   "dolt",
    43  		Short: "Run a git-dolt subcommand",
    44  		Long: `Run a git-dolt subcommand.
    45  Valid subcommands are: fetch, install, link, update.`,
    46  	}
    47  	fakeGitCmd.AddCommand(rootCmd)
    48  
    49  	cmdInstall := &cobra.Command{
    50  		Use:   "install",
    51  		Short: "Installs the git-dolt smudge filter for this Git repository",
    52  		Long: `Installs the git-dolt smudge filter for this Git repository.
    53  After this, when git-dolt pointer files are checked out in this repository, the corresponding Dolt repositories will automatically be cloned.`,
    54  		Args: cobra.NoArgs,
    55  		RunE: func(cmd *cobra.Command, args []string) error {
    56  			return commands.Install()
    57  		},
    58  	}
    59  
    60  	cmdLink := &cobra.Command{
    61  		Use:   "link <remote-url>",
    62  		Short: "Links the given Dolt repository to the current Git repository",
    63  		Long: `Links the given Dolt repository to the current Git repository.
    64  The Dolt repository is cloned in the current directory and added to ./.gitignore, and a git-dolt pointer file is created.`,
    65  		Args: cobra.ExactArgs(1),
    66  		RunE: func(cmd *cobra.Command, args []string) error {
    67  			return commands.Link(args[0])
    68  		},
    69  	}
    70  
    71  	cmdFetch := &cobra.Command{
    72  		Use:   "fetch <pointer-file>",
    73  		Short: "Fetches the Dolt repository referred to in the given git-dolt pointer file",
    74  		Long: `Fetches the Dolt repository referred to in the given git-dolt pointer file.
    75  The Dolt repository is cloned to the current directory and checked out to the revision specified in the git-dolt pointer file.`,
    76  		Args: cobra.ExactArgs(1),
    77  		RunE: func(cmd *cobra.Command, args []string) error {
    78  			return commands.Fetch(args[0])
    79  		},
    80  	}
    81  
    82  	cmdUpdate := &cobra.Command{
    83  		Use:   "update <pointer-file> <revision>",
    84  		Short: "Updates the reference in the given git-dolt pointer file to the given revision",
    85  		Args:  cobra.ExactArgs(2),
    86  		RunE: func(cmd *cobra.Command, args []string) error {
    87  			return commands.Update(args[0], args[1])
    88  		},
    89  	}
    90  
    91  	rootCmd.AddCommand(cmdInstall, cmdLink, cmdFetch, cmdUpdate)
    92  	if err := rootCmd.Execute(); err != nil {
    93  		os.Exit(1)
    94  	}
    95  }