diff --git a/README.md b/README.md index e69de29..3ef12fb 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,51 @@ +# go-i2ptunnel-config + +Command line utility to convert I2P tunnel configurations between Java I2P, i2pd, and go-i2p formats. + +## Features + +- Converts between .config (Java), .conf (i2pd), and .yaml formats +- Format auto-detection +- Validation checks +- Dry-run mode +- No network connectivity required + +## Install + +```bash +go install github.com/go-i2p/go-i2ptunnel-config@latest +``` + +## Usage + +Basic conversion: +```bash +go-i2ptunnel-config -in tunnel.config -out-format yaml +``` + +Validate only: +```bash +go-i2ptunnel-config -in tunnel.config -validate +``` + +Test conversion: +```bash +go-i2ptunnel-config -in tunnel.config -output-format yaml -dry-run +``` + +## Contributing + +1. Fork repository +2. Create feature branch +3. Run `make fmt` +4. Submit pull request + +## Security + +- No network connectivity +- No private key handling +- Configuration files only + +## License + +MIT License \ No newline at end of file diff --git a/lib/convert.go b/lib/convert.go index cdef2ea..bf36e65 100644 --- a/lib/convert.go +++ b/lib/convert.go @@ -7,7 +7,35 @@ import ( "github.com/urfave/cli" ) -// ConvertCommand handles the CLI command for converting tunnel configurations +// ConvertCommand converts the input configuration file to the specified output format. +// It reads the input file, parses it according to the specified input format, validates the configuration, +// and generates the output in the specified format. If the dry-run flag is set, it prints the output to the console +// instead of writing it to a file. +// +// Parameters: +// - c (*cli.Context): The CLI context containing the command-line arguments and flags. +// +// Flags: +// - input-format (string): The format of the input configuration file. +// - output-format (string): The format of the output configuration file. +// - strict (bool): If true, enables strict validation of the configuration. +// - dry-run (bool): If true, prints the output to the console instead of writing it to a file. +// +// Returns: +// - error: An error if any step of the conversion process fails, including reading the input file, +// parsing the input, validating the configuration, or writing the output file. +// +// Errors: +// - "failed to read input file": If the input file cannot be read. +// - "failed to parse input": If the input data cannot be parsed according to the specified format. +// - "validation error": If the configuration fails validation. +// - "failed to generate output": If the output data cannot be generated according to the specified format. +// - "failed to write output file": If the output file cannot be written. +// +// Related: +// - Converter.parseInput +// - Converter.validate +// - Converter.generateOutput func ConvertCommand(c *cli.Context) error { inputFormat := c.StringSlice("input-format")[0] outputFormat := c.StringSlice("output-format")[0] diff --git a/lib/i2pconv.go b/lib/i2pconv.go index 0585a46..5da3bf2 100644 --- a/lib/i2pconv.go +++ b/lib/i2pconv.go @@ -4,7 +4,20 @@ import ( "fmt" ) -// TunnelConfig represents a normalized tunnel configuration +// TunnelConfig represents the configuration for an I2P tunnel. +// It includes various settings such as the tunnel name, type, interface, port, and other options. +// +// Fields: +// - Name: The name of the tunnel (string). +// - Type: The type of the tunnel (string). +// - Interface: The network interface to bind to (string, optional). +// - Port: The port number to bind to (int, optional). +// - PersistentKey: Indicates if the key should be persistent (bool, optional). +// - Description: A description of the tunnel (string, optional). +// - I2CP: A map of I2CP (I2P Control Protocol) options (map[string]interface{}, optional). +// - Tunnel: A map of tunnel-specific options (map[string]interface{}, optional). +// - Inbound: A map of inbound tunnel options (map[string]interface{}, optional). +// - Outbound: A map of outbound tunnel options (map[string]interface{}, optional). type TunnelConfig struct { Name string `yaml:"name"` Type string `yaml:"type"` @@ -18,7 +31,23 @@ type TunnelConfig struct { Outbound map[string]interface{} `yaml:"outbound,omitempty"` } -// generateOutput generates the output based on the format +// generateOutput generates the output for the given TunnelConfig in the specified format. +// +// Parameters: +// - config (*TunnelConfig): The tunnel configuration to be converted. +// - format (string): The desired output format. Supported formats are "properties", "yaml", and "ini". +// +// Returns: +// - ([]byte): The generated output in the specified format. +// - (error): An error if the specified format is unsupported or if there is an issue during generation. +// +// Errors: +// - Returns an error if the specified format is unsupported. +// +// Related: +// - generateJavaProperties +// - generateYAML +// - generateINI func (c *Converter) generateOutput(config *TunnelConfig, format string) ([]byte, error) { switch format { case "properties": @@ -32,7 +61,6 @@ func (c *Converter) generateOutput(config *TunnelConfig, format string) ([]byte, } } -// validate checks the configuration for any errors func (c *Converter) validate(config *TunnelConfig) error { if config.Name == "" { return fmt.Errorf("name is required") @@ -46,10 +74,8 @@ func (c *Converter) validate(config *TunnelConfig) error { // Converter handles configuration format conversions type Converter struct { strict bool - // logger Logger } -// Convert transforms between configuration formats func (c *Converter) Convert(input []byte, inFormat, outFormat string) ([]byte, error) { config, err := c.parseInput(input, inFormat) if err != nil { @@ -63,7 +89,6 @@ func (c *Converter) Convert(input []byte, inFormat, outFormat string) ([]byte, e return c.generateOutput(config, outFormat) } -// parseInput parses the input based on the format func (c *Converter) parseInput(input []byte, format string) (*TunnelConfig, error) { switch format { case "properties": diff --git a/lib/properties.go b/lib/properties.go index 91a47cc..151ca8b 100644 --- a/lib/properties.go +++ b/lib/properties.go @@ -65,6 +65,21 @@ func (c *Converter) parsePropertyKey(k, s string, config *TunnelConfig) { } } +// generateJavaProperties generates a Java properties file content based on the provided TunnelConfig. +// It constructs the properties as a byte slice. +// +// Parameters: +// - config (*TunnelConfig): The configuration for the tunnel. It includes various fields such as Name, Type, Interface, Port, PersistentKey, Description, and maps for I2CP, Tunnel, Inbound, and Outbound options. +// +// Returns: +// - ([]byte): A byte slice containing the generated properties file content. +// - (error): An error if any occurs during the generation process. +// +// Notable Errors/Edge Cases: +// - The function does not handle any specific errors internally but returns any error encountered during the string building process. +// +// Related Code Entities: +// - TunnelConfig: The structure that holds the configuration for the tunnel. func (c *Converter) generateJavaProperties(config *TunnelConfig) ([]byte, error) { var sb strings.Builder