github.com/jchengjr77/canaveral@v1.0.1-0.20200715160102-ea9245d1a2fb/finder/openFinder.go (about)

     1  /*Package finder contains:
     2  	- functionality for opening canaveral project in finder
     3  // ? NOTE: this feature is targeted at macOS users
     4  */
     5  package finder
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  
    13  	"github.com/jchengjr77/canaveral/lib"
    14  )
    15  
    16  // OpenFinder will take a project name and config path,
    17  // and attempt to open the specified project in a new finder window.
    18  // Returns error if it fails. Otherwise returns nil.
    19  // * tested
    20  func OpenFinder(projName string, configPath string) (finalErr error) {
    21  	// defer a recover function that returns the thrown error
    22  	defer func() {
    23  		if r := recover(); r != nil {
    24  			finalErr = r.(error)
    25  		}
    26  	}()
    27  	if projName == "" {
    28  		// Check for blank project name
    29  		fmt.Println("Please provide a project name.")
    30  		fmt.Println("(For more info, 'canaveral --help')")
    31  		return errors.New("No project name specified")
    32  	} else if !lib.FileExists(configPath) {
    33  		// Check that workspace is set
    34  		fmt.Println("No canaveral workspace set. Please specify a workspace.")
    35  		fmt.Println(
    36  			"Canaveral needs to know where to look for your projects.")
    37  		fmt.Println("(For help, type 'canaveral --help')")
    38  		return errors.New("No canaveral workspace set")
    39  	}
    40  	fmt.Printf("Attempting to open Project '%s'\n", projName)
    41  	ws, err := ioutil.ReadFile(configPath)
    42  	lib.Check(err)
    43  	if !lib.DirExists(string(ws) + "/" + projName) {
    44  		// Check that project exists
    45  		return errors.New("Project '" + projName + "' not found")
    46  	}
    47  	err = os.Chdir(string(ws) + "/" + projName)
    48  	lib.Check(err)
    49  	// Open project in file explorer (OS dependent)
    50  	openFinder := openCmd
    51  	// set correct pipes
    52  	openFinder.Stdout = os.Stdout
    53  	openFinder.Stderr = os.Stderr
    54  	err = openFinder.Run()
    55  	lib.Check(err)
    56  	return nil
    57  }