printing: commit some refactors (probably broken)
This commit is contained in:
43
printer/printer.go
Normal file
43
printer/printer.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package printer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/mmcloughlin/avo"
|
||||
)
|
||||
|
||||
type Printer interface {
|
||||
Print(*avo.File) ([]byte, error)
|
||||
}
|
||||
|
||||
type Builder func(Config) Printer
|
||||
|
||||
type Config struct {
|
||||
Name string
|
||||
Argv []string
|
||||
Pkg string
|
||||
}
|
||||
|
||||
func NewDefaultConfig() Config {
|
||||
cfg := Config{
|
||||
Argv: os.Args,
|
||||
}
|
||||
if cwd, err := os.Getwd(); err == nil {
|
||||
cfg.Pkg = filepath.Base(cwd)
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func (c Config) GeneratedBy() string {
|
||||
if c.Argv == nil {
|
||||
return c.Name
|
||||
}
|
||||
return fmt.Sprintf("command: %s", strings.Join(c.Argv, " "))
|
||||
}
|
||||
|
||||
func (c Config) GeneratedWarning() string {
|
||||
return fmt.Sprintf("Code generated by %s. DO NOT EDIT.", c.GeneratedBy())
|
||||
}
|
||||
Reference in New Issue
Block a user