printer/printer.go
author rjp <zimpenfish@gmail.com>
Mon, 23 Jan 2023 16:39:02 +0000
changeset 267 5b91a65ba95a
parent 110 57843255fd1a
permissions -rw-r--r--
Update to handle non-int64 IDs Pleroma/Akkoma and GotoSocial use opaque IDs rather than `int64`s like Mastodon which means that `madon` can't talk to either of those. This commit updates everything that can be an ID to `madon.ActivityID` which is an alias for `string` - can't create a specific type for it since there's more than a few places where they're concatenated directly to strings for URLs, etc. Which means it could just as easily be a direct `string` type itself but I find that having distinct types can often make the code more readable and understandable. One extra bit is that `statusOpts` has grown a `_hasReplyTo` boolean to indicate whether the `--in-reply-to` flag was given or not because we can't distinguish because "empty because default" or "empty because given and empty". Another way around this would be to set the default to some theoretically impossible or unlikely string but you never know when someone might spin up an instance where, e.g., admin posts have negative integer IDs.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net>
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
//
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
// Licensed under the MIT license.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
// Please see the LICENSE file is this directory.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
package printer
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
import (
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
	"fmt"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"io"
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
)
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
83
57afac822019 Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    13
// Options contains options used when creating a ResourcePrinter
57afac822019 Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    14
type Options map[string]string
57afac822019 Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    15
110
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 85
diff changeset
    16
type commonPrinter struct {
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 85
diff changeset
    17
	w io.Writer
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 85
diff changeset
    18
}
57843255fd1a Refactor printers
Mikael Berthe <mikael@lilotux.net>
parents: 85
diff changeset
    19
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
// ResourcePrinter is an interface used to print objects.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
type ResourcePrinter interface {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
	// PrintObj receives a runtime object, formats it and prints it to a writer.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	PrintObj(interface{}, io.Writer, string) error
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
// NewPrinter returns a ResourcePrinter for the specified kind of output.
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
// It returns nil if the output is not supported.
83
57afac822019 Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    28
func NewPrinter(output string, options Options) (ResourcePrinter, error) {
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	switch output {
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	case "", "plain":
83
57afac822019 Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    31
		return NewPrinterPlain(options)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	case "json":
83
57afac822019 Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    33
		return NewPrinterJSON(options)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
	case "yaml":
83
57afac822019 Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    35
		return NewPrinterYAML(options)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
	case "template":
83
57afac822019 Use a map for ResourcePrinter options
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    37
		return NewPrinterTemplate(options)
85
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents: 83
diff changeset
    38
	case "theme":
a4464c0b0c36 Add theme support
Mikael Berthe <mikael@lilotux.net>
parents: 83
diff changeset
    39
		return NewPrinterTheme(options)
0
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
	}
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	return nil, fmt.Errorf("unhandled output format")
5abace724584 Initial public release
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
}