// Write a value or a stream of data to a file at the given path.  The destination path can be a local
// filesystem path, a URI that uses a custom scheme registered outside of the application, or the string
// "temporary", which will write to a temporary file whose path will be returned in the response.
Write a value or a stream of data to a file at the given path.  The destination path can be a local
filesystem path, a URI that uses a custom scheme registered outside of the application, or the string
"temporary", which will write to a temporary file whose path will be returned in the response.
// Write a value or a stream of data to a file at the given path . The destination path can be a local // filesystem path a URI that uses a custom scheme registered outside of the application or the string // " temporary " which will write to a temporary file whose path will be returned in the response .
func (self *Commands) Write(destination interface{}, args *WriteArgs) (*WriteResponse, error) {
	if args == nil {
		args = &WriteArgs{}
	}
	defaults.SetDefaults(args)
	response := &WriteResponse{}
	var writer io.Writer
	if destination != nil {
		if filename, ok := destination.(string); ok {
			if newPath, w, ok := self.browser.GetWriterForPath(filename); ok {
				writer = w
				response.Path = newPath
			} else if filename == `temporary` {
				if temp, err := ioutil.TempFile(``, ``); err == nil {
					writer = temp
					response.Path = temp.Name()
				} else {
					return nil, err
				}
			} else if file, err := os.Create(filename); err == nil {
				writer = file
				response.Path = filename
			} else {
				return nil, err
			}
		} else if w, ok := destination.(io.Writer); ok {
			writer = w
		} else {
			return nil, fmt.Errorf("Unsupported destination %T; expected string or io.Writer", destination)
		}
	}
	if writer == nil {
		return response, fmt.Errorf("A destination must be specified")
	}
	if writer != nil {
		var err error
		if args.Data != nil {
			response.Size, err = io.Copy(writer, args.Data)
		} else if args.Value != nil {
			source := bytes.NewBufferString(fmt.Sprintf("%v", args.Value))
			response.Size, err = io.Copy(writer, source)
		} else {
			err = fmt.Errorf("Must specify source data or a discrete value to write")
		}
		if err == nil {
			if args.Autoclose {
				if closer, ok := writer.(io.Closer); ok {
					return response, closer.Close()
				}
			}
		} else {
			return response, err
		}
	} else {
		return response, fmt.Errorf("Unable to write to destination")
	}
	return response, nil
}
func ( self * Commands ) Write ( destination interface { } , args * WriteArgs ) ( * WriteResponse , error ) { if args == nil { args = & WriteArgs { } } defaults . SetDefaults ( args ) response := & WriteResponse { } var writer io . Writer if destination != nil { if filename , ok := destination . ( string ) ; ok { if newPath , w , ok := self . browser . GetWriterForPath ( filename ) ; ok { writer = w response . Path = newPath } else if filename == `temporary` { if temp , err := ioutil . TempFile ( `` , `` ) ; err == nil { writer = temp response . Path = temp . Name ( ) } else { return nil , err } } else if file , err := os . Create ( filename ) ; err == nil { writer = file response . Path = filename } else { return nil , err } } else if w , ok := destination . ( io . Writer ) ; ok { writer = w } else { return nil , fmt . Errorf ( " " , destination ) } } if writer == nil { return response , fmt . Errorf ( " " ) } if writer != nil { var err error if args . Data != nil { response . Size , err = io . Copy ( writer , args . Data ) } else if args . Value != nil { source := bytes . NewBufferString ( fmt . Sprintf ( " " , args . Value ) ) response . Size , err = io . Copy ( writer , source ) } else { err = fmt . Errorf ( " " ) } if err == nil { if args . Autoclose { if closer , ok := writer . ( io . Closer ) ; ok { return response , closer . Close ( ) } } } else { return response , err } } else { return response , fmt . Errorf ( " " ) } return response , nil }
Write a value or a stream of data to a file at the given path.
Write a value or a stream of data to a file at the given path .
// if whatever write (or write attempt) we just did succeeded... // if we're supposed to autoclose the destination, give that a shot now