### Create Topic Example Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Example of creating a topic using Pulsarctl. ```go } table.Render() ``` - If the output information is a single-line text prompt, the details are as follows: ``` vc.Command.Printf("Create topic %s with %d partitions successfully\n", topic.String(), partitions) ``` ``` -------------------------------- ### Output Configuration Example Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Example of the output flags available for commands supporting output formatting. ```text Output flags: -o, --output string The output format (text,json,yaml) (default "text") ``` -------------------------------- ### Tablewriter Example Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Example of using the 'tablewriter' library to print data in a tabular format. ```go table := tablewriter.NewWriter(vc.Command.OutOrStdout()) table.SetHeader([]string{"Pulsar Function Name"}) for _, f := range functions { table.Append([]string{f}) } ``` -------------------------------- ### Enabling Output Flagset Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Example of how to enable the output flagset for a command. ```go vc.EnableOutputFlagSet() ``` -------------------------------- ### Install bash-completion v2 Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/enable_completion.md Command to install bash-completion v2 using Homebrew. ```bash brew install bash-completion@2 ``` -------------------------------- ### Go Admin API Example Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/overview_of_pulsarctl.md This Go code example demonstrates how to use the Pulsarctl admin API to list clusters in a standalone Pulsar deployment. ```Go package main import ( "fmt" "net/http" "github.com/streamnative/pulsarctl/pkg/pulsar" ) func main() { config := &pulsar.Config{ WebServiceURL: "http://localhost:8080", HTTPClient: http.DefaultClient, // If the server enable the TLSAuth // Auth: auth.NewAuthenticationTLS() // If the server enable the TokenAuth // TokenAuth: auth.NewAuthenticationToken() } // the default NewPulsarClient will use v2 APIs. If you need to request other version APIs, // you can specified the API version like this: // admin := cmdutils.NewPulsarClientWithAPIVersion(pulsar.V2) admin, err := pulsar.New(config) if err != nil { // handle the err return } // more APIs, you can find them in the pkg/pulsar/admin.go // You can find all the method in the pkg/pulsar clusters, err := admin.Clusters().List() if err != nil { // handle the error return } // handle the result fmt.Println(clusters) } ``` -------------------------------- ### Install pulsarctl as project dependency Source: https://github.com/streamnative/pulsarctl/blob/master/README.md Installs pulsarctl as a dependency in a Go project using go get. ```shell # Using master branch go get github.com/streamnative/pulsarctl@master # Or using v2.10.1.3 tag go get github.com/streamnative/pulsarctl@v2.10.1.3 # Or using v2.9.3.3 tag go get github.com/streamnative/pulsarctl@v2.9.3.3 ``` -------------------------------- ### Example plugin Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-extend-pulsarctl-with-plugins.md An example plugin written in bash that can be extended to implement new commands for pulsarctl. ```bash #!/bin/bash if [[ $1 == "args" ]] then echo "I am the args of the pulsarctl-foo" exit 0 fi echo "I am a plugin named pulsarctl-foo" ``` -------------------------------- ### Install Pulsarctl Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/release_process.md Command to install the Pulsarctl to verify the release version. ```shell sh -c "$(curl -fsSL https://raw.githubusercontent.com/streamnative/pulsarctl/master/install.sh)" ``` -------------------------------- ### Install pulsarctl on Mac Source: https://github.com/streamnative/pulsarctl/blob/master/README.md Installs pulsarctl using homebrew on macOS. ```bash brew tap streamnative/streamnative brew install pulsarctl ``` -------------------------------- ### CreateTopicCmd Function Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Defines the command-line interface for creating a topic, including its description, permissions, examples, and output format. ```go func CreateTopicCmd(vc *cmdutils.VerbCmd) { var desc pulsar.LongDescription desc.CommandUsedFor = "This command is used for creating topic." desc.CommandPermission = "This command requires namespace admin permissions." var examples []pulsar.Example createNonPartitions := pulsar.Example{ Desc: "Create a non-partitioned topic ", Command: "Pulsarctl topics create 0", } examples = append(examples, createNonPartitions) desc.CommandExamples = examples var out []pulsar.Output successOut := pulsar.Output{ Desc: "normal output", Out: "Create topic with partitions successfully", } out = append(out, successOut) desc.CommandOutput = out vc.SetDescription( "create", "Create a topic with n partitions", desc.ToString(), desc.ExampleToString(), "c") vc.SetRunFuncWithMultiNameArgs(func() error { return doCreateTopic(vc) }, CheckTopicNameTwoArgs) } ``` -------------------------------- ### Topic Command Implementation Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Example of how to implement a new command group for topics in pulsarctl. ```go func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command { resourceCmd := cmdutils.NewResourceCmd( "topics", "Operations about topic(s)", "", "topic") commands := []func(*cmdutils.VerbCmd){ CreateTopicCmd, } cmdutils.AddVerbCmds(flagGrouping, resourceCmd, commands...) return resourceCmd } ``` -------------------------------- ### Add a new command - Create topic command files Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Example commands to create the necessary Go files for new topic-related commands within the ctl/topic directory. ```bash cd pkg/ctl/topic touch create.go topic.go ``` -------------------------------- ### Error Handling in Tests Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Example of how to intercept and handle errors during testing in pulsarctl. ```go var execError error cmdutils.ExecErrorHandler = func(err error) { execError = err } ``` -------------------------------- ### Fish autocompletion setup (for each session) Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/enable_completion.md Command to load Fish autocompletion for pulsarctl for each session. ```bash pulsarctl completion fish > ~/.config/fish/completions/pulsarctl.fish ``` -------------------------------- ### Setting Flags for Topic Creation Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Example of how to set flags for topic creation within a Pulsarctl command. ```go vc.FlagSetGroup.InFlagSet("Topic", func(set *pflag.FlagSet) { set.BoolVarP(&partition, "partitioned-topic", "p", false, "Get the partitioned topic stats") // other flags }) ``` -------------------------------- ### Zsh autocompletion setup Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/enable_completion.md Commands to set up Zsh autocompletion for pulsarctl. ```bash mkdir -p ~/.zsh/completion/ pulsarctl completion zsh > ~/.zsh/completion/_pulsarctl ``` ```bash fpath=($fpath ~/.zsh/completion) source ~/.zshrc ``` ```bash rm -f ~/.zcompdump; compinit ``` -------------------------------- ### Fish autocompletion setup (current session) Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/enable_completion.md Command to load Fish autocompletion for pulsarctl in the current session. ```bash pulsarctl completion fish | source ``` -------------------------------- ### Enable TLS in broker configuration Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/overview_of_pulsarctl.md Example configuration to enable TLS in a Pulsar broker. ```properties # Enable TLS tlsEnabled=true tlsCertificateFilePath=/test/auth/certs/broker-cert.pem tlsKeyFilePath=/test/auth/certs/broker-key.pem tlsTrustCertsFilePath=/test/auth/certs/cacert.pem tlsAllowInsecureConnection=false ``` -------------------------------- ### Get pulsarctl version Source: https://github.com/streamnative/pulsarctl/blob/master/README.md Prints the version of the built pulsarctl binary. ```bash bin/pulsarctl --version ``` -------------------------------- ### Add a new command - Create topic folder Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Example command to create a new directory for topic-related commands under the ctl directory. ```bash mkdir topic ``` -------------------------------- ### Contribution flow example Source: https://github.com/streamnative/pulsarctl/blob/master/CONTRIBUTING.md This bash script outlines the steps for contributing code, including forking, syncing with the remote master, creating a new branch, making changes, committing, and pushing for a pull request. ```bash $ git remote add streamnative https://github.com/streamnative/pulsarctl.git # sync with the remote master $ git checkout master $ git fetch streamnative $ git rebase streamnative/master $ git push origin master # create a PR branch $ git checkout -b your_branch # do something $ git add [your change files] $ git commit -sm "xxx" $ git push origin your_branch ``` -------------------------------- ### Get current context Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-use-context.md Command to display the current context and available cluster information. ```bash $ pulsarctl context current ``` -------------------------------- ### Topics Create Implementation Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Implements the Create method for the Topics interface, handling partitioned and non-partitioned topics. ```go func (t *topics) Create(topic TopicName, partitions int) error { endpoint := t.client.endpoint(t.basePath, topic.GetRestPath(), "partitions") if partitions == 0 { endpoint = t.client.endpoint(t.basePath, topic.GetRestPath()) } return t.client.put(endpoint, partitions, nil) } ``` -------------------------------- ### Define contexts and auth info Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-use-context.md Commands to set up development and scratch clusters with their respective service URLs. ```bash $ pulsarctl context set development --admin-service-url="http://1.2.3.4:8080" --bookie-service-url="http://1.2.3.4:8083" $ pulsarctl context set scratch --admin-service-url="http://5.6.7.8:8080" --bookie-service-url="http://5.6.7.8:8083" ``` -------------------------------- ### Pulsar Client API Versions Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Demonstrates how to create a Pulsar client with default or custom API versions. ```go // default value, use the V2 version NewPulsarClient() // custom version NewPulsarClientWithApiVersion(version pulsar.ApiVersion) ``` -------------------------------- ### doCreateTopic Function Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Implements the logic to create a topic using the Pulsar client. ```go func doCreateTopic(vc *cmdutils.VerbCmd) error { admin := cmdutils.NewPulsarClient() err = admin.Topics().Create(*topic, partitions) if err == nil { vc.Command.Printf("Create topic %s with %d partitions successfully\n", topic.String(), partitions) } return err } ``` -------------------------------- ### List all contexts Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-use-context.md Command to list all defined contexts with their details. ```bash $ pulsarctl context get ``` -------------------------------- ### Build pulsarctl Source: https://github.com/streamnative/pulsarctl/blob/master/README.md Builds the pulsarctl binary using make. ```bash make pulsarctl ``` -------------------------------- ### Make the plugin executable Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-extend-pulsarctl-with-plugins.md Make the plugin executable using chmod +x. ```bash chmod +x ./pulsarctl-foo ``` -------------------------------- ### Download dependencies for pulsarctl Source: https://github.com/streamnative/pulsarctl/blob/master/README.md Downloads project dependencies using go mod. ```bash go mod download ``` -------------------------------- ### Check content of pulsar configuration Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-use-context.md Command to view the content of the pulsar configuration file. ```bash $ cat $HOME/.config/pulsar ``` -------------------------------- ### Move the plugin to PATH Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-extend-pulsarctl-with-plugins.md Move the plugin to a directory in your PATH. ```bash mv pulsarctl-foo /usr/local/bin ``` -------------------------------- ### Configure JWT in broker.conf or standalone.conf Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/overview_of_pulsarctl.md This snippet shows how to enable JWT authentication by configuring broker or standalone configuration files. ```properties # Enable JWT # Enable authentication authenticationEnabled=true # Autentication provider name list, which is comma separated list of class names authenticationProviders=org.apache.pulsar.broker.authentication.AuthenticationProviderToken # Configure the secret key to be used to validate auth tokens tokenSecretKey=file:///test/auth/token/secret.key ``` -------------------------------- ### Project Structure Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md The directory structure of the pulsarctl project, showing the organization of packages for different functionalities. ```bash ├── docs ├── pkg │ ├── auth │ ├── cmdutils │ ├── ctl │ │ ├── cluster │ │ ├── completion │ │ ├── functions │ │ ├── namespace │ │ ├── schemas │ │ ├── sinks │ │ ├── sources │ │ ├── tenant │ │ ├── topic │ │ └── utils │ └── pulsar ├── site └── test ``` -------------------------------- ### Configure bashrc for bash-completion Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/enable_completion.md Lines to add to ~/.bashrc to configure bash-completion. ```bash export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d" [[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh" ``` -------------------------------- ### Invoke the plugin with arguments Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-extend-pulsarctl-with-plugins.md Invoke the plugin with arguments. ```bash pulsarctl foo args ``` -------------------------------- ### Topics Interface Definition Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md Defines the interface for topic-related operations in the Pulsar admin client. ```go type Topics interface { Create(TopicName, int) error } ``` -------------------------------- ### Set the current context Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-use-context.md Command to switch the active context to the 'scratch' cluster. ```bash $ pulsarctl context use scratch ``` -------------------------------- ### Add a new command - Usage Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/developer-guide.md The general usage format for the pulsarctl command-line tool. ```bash pulsarctl [commands] [sub commands] [flags] ``` -------------------------------- ### pulsarctl command syntax Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/overview_of_pulsarctl.md The general syntax for running a pulsarctl command. ```bash pulsarctl [resource] [command] [name] [flags] ``` -------------------------------- ### Invoke the plugin Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-extend-pulsarctl-with-plugins.md Invoke the plugin as a kubectl command. ```bash pulsarctl foo ``` -------------------------------- ### Rename a context Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-use-context.md Command to rename an existing context. ```bash $ pulsarctl context rename old_name new_name ``` -------------------------------- ### Source pulsarctl completion script in bashrc Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/enable_completion.md Command to source the pulsarctl completion script in ~/.bashrc. ```bash echo 'source /usr/local/etc/bash_completion.d/pulsarctl.bash' >> ~/.bashrc source ~/.bashrc ``` -------------------------------- ### Clone pulsarctl repository Source: https://github.com/streamnative/pulsarctl/blob/master/README.md Clones the pulsarctl project from GitHub. ```bash git clone https://github.com/streamnative/pulsarctl.git ``` -------------------------------- ### Create release branch and tag Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/release_process.md Steps to create a release branch from master and then create a tag for the release. ```shell git clone git@github.com:streamnattive/pulsarctl.git cd pulsarctl # Create a branch git checkout -b branch-x.y.z origin/master # Create a tag git tag -u $USER@streamnative.io vx.y.z -m 'Release vx.y.z' ``` -------------------------------- ### Push release branch and tag Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/release_process.md Push both the release branch and the tag to the Github repository. ```shell # Push both the branch and the tag to Github repo git push origin branch-x.y.z git push origin vx.y.z ``` -------------------------------- ### Delete a context Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/how-to-use-context.md Command to delete a specified context. ```bash $ pulsarctl context delete scratch ``` -------------------------------- ### Enable pulsarctl autocompletion for Bash Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/enable_completion.md Command to enable pulsarctl autocompletion for Bash by adding the script to bash_completion.d. ```bash pulsarctl completion bash >/usr/local/etc/bash_completion.d/pulsarctl.bash ``` -------------------------------- ### Pulsarctl common flags for TLS Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/overview_of_pulsarctl.md This snippet shows the common flags available in pulsarctl for configuring TLS connections. ```bash pulsarctl \ --admin-service-url https://localhost:8443 \ --auth-params "{\"tlsCertFile\":\"/test/auth/certs/client-cert.pem\",\"tlsKeyFile\":\"/test/auth/certs/client-key.pem\"}" \ --tls-allow-insecure \ --tls-trust-cert-path /test/auth/certs/cacert.pem \ topics list public/default ``` -------------------------------- ### Pulsarctl flags for JWT authentication Source: https://github.com/streamnative/pulsarctl/blob/master/docs/en/overview_of_pulsarctl.md This snippet shows the pulsarctl flags used for JWT authentication, either as a string or from a file. ```bash pulsarctl \ --token "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.Yb52IE0B5wzooAdSlIlskEgb6_HBXST8k3lINZS5wwg" \ topics list public/default ``` ```bash pulsarctl \ --token-file file:///test/auth/tokens/token topics list public/default ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.