github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/cmd/git-dolt/doltops/doltops.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 doltops contains functions for performing dolt operations 16 // using the CLI. 17 package doltops 18 19 import ( 20 "bufio" 21 "fmt" 22 "os/exec" 23 24 "github.com/dolthub/dolt/go/cmd/git-dolt/utils" 25 ) 26 27 // Clone clones the specified dolt remote, streaming the output from dolt clone to stdout. 28 func Clone(remote string) error { 29 cmd := exec.Command("dolt", "clone", remote) 30 if err := runAndStreamOutput(cmd, "dolt clone"); err != nil { 31 return err 32 } 33 return nil 34 } 35 36 // CloneToRevision clones the specified dolt remote and checks it out to the specified revision. 37 // It streams the output from dolt clone and dolt checkout to stdout. 38 func CloneToRevision(remote string, revision string) error { 39 if err := Clone(remote); err != nil { 40 return err 41 } 42 43 dirname := utils.LastSegment(remote) 44 checkoutCmd := exec.Command("dolt", "checkout", "-b", "git-dolt-pinned", revision) 45 checkoutCmd.Dir = dirname 46 if err := runAndStreamOutput(checkoutCmd, "dolt checkout"); err != nil { 47 return err 48 } 49 50 return nil 51 } 52 53 func runAndStreamOutput(cmd *exec.Cmd, name string) error { 54 cmdReader, err := cmd.StdoutPipe() 55 if err != nil { 56 return fmt.Errorf("error creating StdoutPipe for %s: %v", name, err) 57 } 58 59 scanner := bufio.NewScanner(cmdReader) 60 go func() { 61 for scanner.Scan() { 62 fmt.Println(scanner.Text()) 63 } 64 }() 65 66 if err := cmd.Start(); err != nil { 67 return fmt.Errorf("error starting %s: %v", name, err) 68 } 69 70 if err := cmd.Wait(); err != nil { 71 return fmt.Errorf("error waiting for %s: %v", name, err) 72 } 73 74 return nil 75 } 76 77 // CloneToRevisionSilent clones the specified dolt remote and checks it out to the specified revision, 78 // suppressing all output from dolt clone and dolt checkout. 79 func CloneToRevisionSilent(remote string, revision string) error { 80 if err := exec.Command("dolt", "clone", remote).Run(); err != nil { 81 return fmt.Errorf("error cloning remote repository at %s: %v", remote, err) 82 } 83 84 dirname := utils.LastSegment(remote) 85 checkoutCmd := exec.Command("dolt", "checkout", "-b", "git-dolt-pinned", revision) 86 checkoutCmd.Dir = dirname 87 if err := checkoutCmd.Run(); err != nil { 88 return fmt.Errorf("error checking out revision %s in directory %s: %v", revision, dirname, err) 89 } 90 91 return nil 92 }