GithubHelp home page GithubHelp logo

andriusphoto / go-drupal Goto Github PK

View Code? Open in Web Editor NEW

This project forked from phayes/go-drupal

0.0 0.0 0.0 26 KB

Interface with a Drupal site using golang, execute drush commands

PHP 60.02% Go 39.98%

go-drupal's Introduction

Go Drupal

GoDoc Go Report Card

Package drupal is a go library for interacting with a drupal site via command-line and drush.

Get simple site status information:

func main() {
	site, err := drupal.NewSite("/var/www/drupalsite")
	if err != nil {
		log.Fatal(err)
	}

	status, err := site.GetStatus();
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(status.DrupalVersion)
}

Connect to the Drupal database:

package main

import (
	"fmt"
	"log"
)

func main() {
	site, err := drupal.NewSite("/var/www/drupalsite")
	if err != nil {
		log.Fatal(err)
	}

	dbinfo, err := site.GetDatabase()
	if err != nil {
		log.Fatal(err)
	}

	db, err := dbinfo.Open()
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Test the connection to the database
	err = db.Ping()
	if err != nil {
		log.Fatal(err)
	}

	// Do queries
	var user string
	err := db.QueryRow("SELECT name FROM users WHERE uid=?", 1).Scan(&user)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Admin username is", user)
}

Running a drush command:

package main

import (
	"fmt"
	"log"
)

func main() {
	site, err := drupal.NewSite("/var/www/drupalsite/sites/multisite")
	if err != nil {
		log.Fatal(err)
	}

	// Run "drush cr" to rebuild cache
	output, messages, err := site.Drush("cr")
	if err != nil {
		// Optionally inspect the error to see if they are only warnings
		errset, ok := err.(drupal.DrushMessages)
		if !ok {
			log.Fatal(err) // Error running the drush command
		}
		// If it contains errors, then throw fatal error
		if errset.HasErrors() {
			log.Fatal(errset)
		}
		// If it has no errors, but has warnings, just print the warnings
		if !errset.HasErrors() && errset.HasWarnings() {
			fmt.Println(errset)
		}
		// If it doesn't contain warnings or errors, do nothing
		// This might be the case if it produces notices or other unknown stderr output
		if !errset.HasErrors() && !errset.HasWarnings() {
			// do nothing
		}
	}

	// Print out any "ok" or "success" messages
	for _, message := range messages {
		fmt.Println(message)
	}

	// Print out the output
	fmt.Println(output)
}

Get $settings from settings.php:

package main

import (
	"fmt"
	"log"
)

func main() {
	site, err := drupal.NewSite("/var/www/drupalsite")
	if err != nil {
		log.Fatal(err)
	}

	settings, err := site.GetSettings()
	if err != nil {
		log.Fatal(err)
	}

	// Get a specific setting
	hashSalt := settings.GetString("hash_salt")

	fmt.Println("Hash Salt for site is", hashSalt)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.