func cmdEdit(opts map[string]interface{}, conf config.Config) {

	cheatsheet := opts["--edit"].(string)

	// load the cheatsheets
	cheatsheets, err := sheets.Load(conf.Cheatpaths)
	if err != nil {
		fmt.Fprintln(os.Stderr, fmt.Sprintf("failed to list cheatsheets: %v", err))
		os.Exit(1)
	}

	// filter cheatcheats by tag if --tag was provided
	if opts["--tag"] != nil {
		cheatsheets = sheets.Filter(
			cheatsheets,
			strings.Split(opts["--tag"].(string), ","),
		)
	}

	// consolidate the cheatsheets found on all paths into a single map of
	// `title` => `sheet` (ie, allow more local cheatsheets to override less
	// local cheatsheets)
	consolidated := sheets.Consolidate(cheatsheets)

	// the file path of the sheet to edit
	var editpath string

	// determine if the sheet exists
	sheet, ok := consolidated[cheatsheet]

	// if the sheet exists and is not read-only, edit it in place
	if ok && !sheet.ReadOnly {
		editpath = sheet.Path

		// if the sheet exists but is read-only, copy it before editing
	} else if ok && sheet.ReadOnly {
		// compute the new edit path
		// begin by getting a writeable cheatpath
		writepath, err := cheatpath.Writeable(conf.Cheatpaths)
		if err != nil {
			fmt.Fprintf(os.Stderr, "failed to get writeable path: %v\n", err)
			os.Exit(1)
		}

		// compute the new edit path
		editpath = path.Join(writepath.Path, sheet.Title)

		// create any necessary subdirectories
		dirs := path.Dir(editpath)
		if dirs != "." {
			if err := os.MkdirAll(dirs, 0755); err != nil {
				fmt.Fprintf(os.Stderr, "failed to create directory: %s, %v\n", dirs, err)
				os.Exit(1)
			}
		}

		// copy the sheet to the new edit path
		err = sheet.Copy(editpath)
		if err != nil {
			fmt.Fprintf(os.Stderr, "failed to copy cheatsheet: %v\n", err)
			os.Exit(1)
		}

		// if the sheet does not exist, create it
	} else {
		// compute the new edit path
		// begin by getting a writeable cheatpath
		writepath, err := cheatpath.Writeable(conf.Cheatpaths)
		if err != nil {
			fmt.Fprintf(os.Stderr, "failed to get writeable path: %v\n", err)
			os.Exit(1)
		}

		// compute the new edit path
		editpath = path.Join(writepath.Path, cheatsheet)

		// create any necessary subdirectories
		dirs := path.Dir(editpath)
		if dirs != "." {
			if err := os.MkdirAll(dirs, 0755); err != nil {
				fmt.Fprintf(os.Stderr, "failed to create directory: %s, %v\n", dirs, err)
				os.Exit(1)
			}
		}
	}

	// split `conf.Editor` into parts to separate the editor's executable from
	// any arguments it may have been passed. If this is not done, the nearby
	// call to `exec.Command` will fail.
	parts := strings.Fields(conf.Editor)
	editor := parts[0]
	args := append(parts[1:], editpath)

	// edit the cheatsheet
	cmd := exec.Command(editor, args...)
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		fmt.Fprintf(os.Stderr, "failed to edit cheatsheet: %v\n", err)
		os.Exit(1)
	}
}
// cmdEdit opens a cheatsheet for editing (or creates it if it doesn't exist).
cmdEdit opens a cheatsheet for editing (or creates it if it doesn't exist).
// cmdEdit opens a cheatsheet for editing ( or creates it if it doesn ' t exist ) .
func cmdEdit(opts map[string]interface{}, conf config.Config) {
	cheatsheet := opts["--edit"].(string)
	cheatsheets, err := sheets.Load(conf.Cheatpaths)
	if err != nil {
		fmt.Fprintln(os.Stderr, fmt.Sprintf("failed to list cheatsheets: %v", err))
		os.Exit(1)
	}
	if opts["--tag"] != nil {
		cheatsheets = sheets.Filter(
			cheatsheets,
			strings.Split(opts["--tag"].(string), ","),
		)
	}
	consolidated := sheets.Consolidate(cheatsheets)
	var editpath string
	sheet, ok := consolidated[cheatsheet]
	if ok && !sheet.ReadOnly {
		editpath = sheet.Path
	} else if ok && sheet.ReadOnly {
		writepath, err := cheatpath.Writeable(conf.Cheatpaths)
		if err != nil {
			fmt.Fprintf(os.Stderr, "failed to get writeable path: %v\n", err)
			os.Exit(1)
		}
		editpath = path.Join(writepath.Path, sheet.Title)
		dirs := path.Dir(editpath)
		if dirs != "." {
			if err := os.MkdirAll(dirs, 0755); err != nil {
				fmt.Fprintf(os.Stderr, "failed to create directory: %s, %v\n", dirs, err)
				os.Exit(1)
			}
		}
		err = sheet.Copy(editpath)
		if err != nil {
			fmt.Fprintf(os.Stderr, "failed to copy cheatsheet: %v\n", err)
			os.Exit(1)
		}
	} else {
		writepath, err := cheatpath.Writeable(conf.Cheatpaths)
		if err != nil {
			fmt.Fprintf(os.Stderr, "failed to get writeable path: %v\n", err)
			os.Exit(1)
		}
		editpath = path.Join(writepath.Path, cheatsheet)
		dirs := path.Dir(editpath)
		if dirs != "." {
			if err := os.MkdirAll(dirs, 0755); err != nil {
				fmt.Fprintf(os.Stderr, "failed to create directory: %s, %v\n", dirs, err)
				os.Exit(1)
			}
		}
	}
	parts := strings.Fields(conf.Editor)
	editor := parts[0]
	args := append(parts[1:], editpath)
	cmd := exec.Command(editor, args...)
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		fmt.Fprintf(os.Stderr, "failed to edit cheatsheet: %v\n", err)
		os.Exit(1)
	}
}
func cmdEdit ( opts map [ string ] interface { } , conf config . Config ) { cheatsheet := opts [ " " ] . ( string ) cheatsheets , err := sheets . Load ( conf . Cheatpaths ) if err != nil { fmt . Fprintln ( os . Stderr , fmt . Sprintf ( " " , err ) ) os . Exit ( 1 ) } if opts [ " " ] != nil { cheatsheets = sheets . Filter ( cheatsheets , strings . Split ( opts [ " " ] . ( string ) , " " ) , ) } consolidated := sheets . Consolidate ( cheatsheets ) var editpath string sheet , ok := consolidated [ cheatsheet ] if ok && ! sheet . ReadOnly { editpath = sheet . Path } else if ok && sheet . ReadOnly { writepath , err := cheatpath . Writeable ( conf . Cheatpaths ) if err != nil { fmt . Fprintf ( os . Stderr , " \n " , err ) os . Exit ( 1 ) } editpath = path . Join ( writepath . Path , sheet . Title ) dirs := path . Dir ( editpath ) if dirs != " " { if err := os . MkdirAll ( dirs , 0755 ) ; err != nil { fmt . Fprintf ( os . Stderr , " \n " , dirs , err ) os . Exit ( 1 ) } } err = sheet . Copy ( editpath ) if err != nil { fmt . Fprintf ( os . Stderr , " \n " , err ) os . Exit ( 1 ) } } else { writepath , err := cheatpath . Writeable ( conf . Cheatpaths ) if err != nil { fmt . Fprintf ( os . Stderr , " \n " , err ) os . Exit ( 1 ) } editpath = path . Join ( writepath . Path , cheatsheet ) dirs := path . Dir ( editpath ) if dirs != " " { if err := os . MkdirAll ( dirs , 0755 ) ; err != nil { fmt . Fprintf ( os . Stderr , " \n " , dirs , err ) os . Exit ( 1 ) } } } parts := strings . Fields ( conf . Editor ) editor := parts [ 0 ] args := append ( parts [ 1 : ] , editpath ) cmd := exec . Command ( editor , args ... ) cmd . Stdout = os . Stdout cmd . Stdin = os . Stdin cmd . Stderr = os . Stderr if err := cmd . Run ( ) ; err != nil { fmt . Fprintf ( os . Stderr , " \n " , err ) os . Exit ( 1 ) } }
cmdEdit opens a cheatsheet for editing (or creates it if it doesn't exist).
cmdEdit opens a cheatsheet for editing ( or creates it if it doesn ' t exist ) .
// load the cheatsheets // filter cheatcheats by tag if --tag was provided // consolidate the cheatsheets found on all paths into a single map of // `title` => `sheet` (ie, allow more local cheatsheets to override less // local cheatsheets) // the file path of the sheet to edit // determine if the sheet exists // if the sheet exists and is not read-only, edit it in place // if the sheet exists but is read-only, copy it before editing // compute the new edit path // begin by getting a writeable cheatpath // compute the new edit path // create any necessary subdirectories // copy the sheet to the new edit path // if the sheet does not exist, create it // compute the new edit path // begin by getting a writeable cheatpath // compute the new edit path // create any necessary subdirectories // split `conf.Editor` into parts to separate the editor's executable from // any arguments it may have been passed. If this is not done, the nearby // call to `exec.Command` will fail. // edit the cheatsheet